从没有UIToolbarButton的UITextField中关闭数字小键盘

时间:2013-01-09 17:40:31

标签: iphone ios objective-c

我知道这里有一些关于How to show button ‘Done’ on number pad on iPhone OS 4?解雇数字键盘的讨论。我想找到一种解决数字键盘的方法,无需添加UIToolbarButton。我的UITextFields是zipcode和age.minin.com iPhone应用程序这样做;所以必须有办法。

这是我当前添加UIToolbarButton的代码:

textField1.keyboardType = UIKeyboardTypeNumberPad;
textField1.delegate = self;

//Create a toolbar to add to the textpad
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleDefault;
numberToolbar.items = [NSArray arrayWithObjects:
                       [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                       [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(dismissZipKeyPad)],
                       nil];
[numberToolbar sizeToFit];
textField1.inputAccessoryView = numberToolbar;

1 个答案:

答案 0 :(得分:0)

当用户触摸文本字段之外的任何地方时,我用它来关闭键盘:

- (void)touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event {
for (UIView* view in self.view.subviews) {
if ([view isKindOfClass:[UITextField class]])
[view resignFirstResponder];
}
}

我在这里找到了它:

http://remarkablepixels.com/blog/2011/1/23/dismiss-iphone-keyboard.html