知道显示哪个键盘

时间:2013-08-14 11:57:39

标签: iphone ios notifications nsnotificationcenter uikeyboard

我正在构建一个应用程序,UITextField显示十进制键盘,当用户点按它时。

我想在键盘上方添加一个“完成”按钮,所以我想使用它:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

它可以很好地检测键盘何时上升,但我的问题是我在同一UIView上有其他文本字段,但我不想在它们上添加这个“完成”按钮。

有没有办法检测它是显示的十进制键盘还是常规键盘?

非常感谢你的帮助!

2 个答案:

答案 0 :(得分:1)

使用if-statement检查屏幕上的UITextField键盘:

 if ([yourTextField isFirstResponder])
 {
       //Add done button
 }

答案 1 :(得分:1)

您需要使用textfied

的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;
}