1

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

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

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

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

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

非常感谢您的帮助!

4

2 回答 2

1

使用 anif-statement检查哪个 UITextField 的键盘在屏幕上:

 if ([yourTextField isFirstResponder])
 {
       //Add done button
 }
于 2013-08-14T12:01:58.900 回答
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;
}
于 2013-08-14T12:11:21.933 回答