好吧,我会尽力解释这一点。我有一个 iPhone 应用程序,它有一个文本字段,用户只能输入数字。那里不是问题。但是,数字键盘上没有完成按钮,所以我无法让它消失。我可以制作一个按钮,用户按下该按钮即可关闭键盘,但我宁愿有一个完成按钮,因为屏幕按原样“忙”。
好吧,经过一番研究,我发现this http://www.neoos.ch/news/46-development/54-uikeyboardtypenumberpad-and-the-missing-return-key。哇,效果很好。但是,我还有另一个需要默认键盘的文本字段。每当键盘出现时,无论哪种类型,它都会显示“完成”按钮。
不好。
所以我做了更多挖掘。仔细阅读评论,人们提到了一种使用“编辑已开始”特征来摆脱“完成”按钮的方法,仅在必要时调用“完成按钮”代码。我也基本上完成了。如果您在数字字段中键入,然后关闭键盘,然后在普通字段中键入,则不会出现“完成”按钮。
但是,存在一个错误,即“完成”按钮仍然出现。如果您点击数字字段,然后点击普通字段,键盘永远不会“消失”,因此即使它从数字键盘变为普通键盘,按钮仍然存在。我想从视图中删除按钮,但我不知道该怎么做。这是我的代码...
//Done button for numpad
- (void)keyboardWillShow:(NSNotification *)note {
if (showDoneButton == YES)
{
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] hasPrefix:@"3"]) {
[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 view found; add the custom button to it
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
showDoneButton = NO; //This tells done button code to run or not
}
}
- (void)doneButton:(id)sender {
NSLog(@"Input: %@", playerOneLifeLabel.text);
NSLog(@"Input: %@", playerTwoLifeLabel.text);
[playerOneLifeLabel resignFirstResponder];
[playerTwoLifeLabel resignFirstResponder];
}
当 NumPadfield 触发“编辑已开始”时,它会调用此函数:
- (IBAction)needNumberPad:(id)sender{
showDoneButton = YES;
}
然后确保显示完成按钮。当完成按钮代码完成时,变量(如上所示)被设置回“否”,因此如果您点击默认文本字段,它不会再次显示。默认情况下,布尔变量设置为 NO。
需要发生的是,如果您立即从编辑 numPad 字段跳转到默认字段,则“完成”按钮会消失。我可以将代码放入“编辑结束”时调用的函数中,但我不知道该放在那里。帮助将不胜感激,我已经走到这一步了!
谢谢你!