我有一个文本字段,我需要用户只键入数值。我正在使用数字键盘类型。在这个键盘上没有完成按钮。我看到一篇文章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)
我只是将我的收入添加到我的veiwconroller中。是否应该添加其他任何操作?这是我的出口
@property(nonatomic, retain) IBOutlet UITextField *anualIncome;
答案 0 :(得分:1)
您可以使用inputAccessoryView
实例的UITextField
属性设置UIToolbar
上的任何按钮。 (实际上它可以是任何UIView子类)当您在任何Web表单中填充一些文本时,生成的视图将与Safari中的一样。这更容易。如果苹果改变他们的实施,也没有机会打破观点...
快乐的编码......
答案 1 :(得分:0)
不使用完成按钮,您可以这样隐藏键盘:
在viewContoller类中实现touches begin方法,如下所示:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[anualIncome resignFirstResponder];
}