我有一个文本字段,我需要用户在其中仅键入数值。我正在使用数字键盘类型。在此键盘中没有完成按钮。我看到一篇文章here 这让我可以在键盘上添加一个完成按钮。
然而,我无法隐藏这一点。这是我的代码
- (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
[doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {
[doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
}
[doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
}
- (void)keyboardWillShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {
[self addButtonToKeyboard];
}
}
- (void)keyboardDidShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[self addButtonToKeyboard];
}
}
- (void)doneButton:(id)sender {
NSLog(@"doneButton");
[anualIncome setText:@"hai"];
NSLog(@"Input: %@", anualIncome.text);
[anualIncome resignFirstResponder];
}
请指导我哪里做错了。当我按下完成按钮时,我可以看到这样的控制台输出
Hello World[2542:f803] doneButton
Hello World[2542:f803] Input: (null)
我只是将我的 anualincome 添加到我的 veiwconroller 中。是否有任何其他操作应该添加?这是我的导出
@property(nonatomic, retain) IBOutlet UITextField *anualIncome;
最佳答案
您可以使用 UITextField
实例的 inputAccessoryView
属性来设置带有任何按钮的 UIToolbar
。 (实际上它可以是任何 UIView 子类)当您在任何 Web 表单中填写一些文本时,生成的 View 将与 Safari 中的一样。这要容易得多。如果苹果改变他们的实现,也没有机会打破观点......
快乐编码...
关于iphone - 如何隐藏数字小键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8396778/