2 sachinks SachinKS 于 2013.08.15 14:48 提问

IOS-判断显示的哪个键盘

使用UITextField创建了一个应用,在用户输入的时候显示十进键盘。

我想自己添加完成键:

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

键盘运行正常,但是问题是在UIView中有另一个textField,我不想在这个添加done按钮。

怎么检测十进键盘和常规键盘?

1个回答

xinyoulingxi1_tong
xinyoulingxi1_tong   2013.08.15 15:24
已采纳

使用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;
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!