使用UITextField
創建了一個應用,在用戶輸入的時候顯示十進鍵盤。
我想自己添加完成鍵:
[ [NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
鍵盤運行正常,但是問題是在UIView中有另一個textField,我不想在這個添加done按鈕。
怎麼檢測十進鍵盤和常規鍵盤?
使用textField的inputAccessoryView
屬性:
txtfld.keyboardType=UIKeyboardTypeDecimalPad;
txtfld.inputAccessoryView=[self toolBarForKeyboardAccessory];
然後:
-(UIToolbar *)toolBarForKeyboardAccessory
{
UIToolbar *keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
keyboardToolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonClicked:)];
CGRect rect = CGRectMake(1, 5.0, 250, 30);
KeyboardTextField = [[UITextField alloc] initWithFrame:rect];
KeyboardTextField.borderStyle = UITextBorderStyleRoundedRect;
KeyboardTextField.font = [UIFont systemFontOfSize:17.0];
KeyboardTextField.delegate = self;
UIBarButtonItem *textFieldItem = [[UIBarButtonItem alloc] initWithCustomView:KeyboardTextField] ;
[keyboardToolbar setItems:[NSArray arrayWithObjects: flexSpace,textFieldItem,flexSpace, cancelButton,flexSpace, nil] animated:NO];
return keyboardToolbar;
}