textFieldShouldEndEditing被调用多次
我正在研究一个包含多个UITextField
对象的视图。我的视图控制器用作UITextFieldDelegate
,并且我实现了(BOOL)textFieldShouldEndEditing:(UITextField *)textField
方法来保存和验证正在显示的记录。textFieldShouldEndEditing被调用多次
如果用户在编辑项目并点击保存/验证失败后点击“完成”按钮,则会显示UIAlertView
,并且用户保留在验证失败的UITextField
上。
我的问题是这样的 - 当用户从UITextField
,将节省/验证失败到另一个UITextField
S的点击,则(BOOL)textFieldShouldEndEditing:(UITextField *)textField
方法被调用多次,和UIAlertView
多次弹出。
为什么(BOOL)textFieldShouldEndEditing:(UITextField *)textField
在用户点击键盘上的“完成”时调用一次,但当用户点击另一个时调用多次UITextField
?
这里是我的代码:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSLog(@"textFieldShouldEndEditing called by textField with text=%@", textField.text);
currentItem.nameOrNumber = nameOrNumber.text;
// Try to save the managed object.
NSError *error = nil;
if (![[currentItem managedObjectContext] save:&error]) {
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Uh Oh!",@"")
message:[error localizedDescription]
delegate:self
cancelButtonTitle:NSLocalizedString(@"OK",@"")
otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
shouldEnd = NO;
}
return shouldEnd;
}
我认为你的问题来自于,当您正在编辑一个文本框文本框方法被调用,直接挖掘到另一个的顺序。
如果我没有记错的话,应该是这样的(你正在编辑在A和点按B)
-
textFieldShouldBeginEditing
场B -
textFieldShouldEndEditing
的领域的 -
textFieldDidEndEditing
现场一个 -
textFieldDidBeginEditing
场B
所以当哟你在textFieldShouldEndEditing
方法中,textfield B已经成为第一响应者。所以当你让UIAlertView出现时,B会失去焦点,因此也会调用textFieldShouldEndEditing
!
这对我来说也是一个问题,当我想在textField开始编辑时提出一个视图。我找到的解决方案是创建一个布尔类变量,指示我是否正在从一个textField切换到另一个。 我将它设置为textFieldShouldBeginEditing
中的TRUE
和textFieldDidBeginEditing
中的FALSE
。当您在textFieldShouldEndEditing
是,如果它被设置为TRUE
这意味着用户直接拍了拍另一个文本框。然后,你只需要找到正确的方法,使您的测试只有一次(也许shouldEndEditing应该返回false或东西)。
看起来适合我被称为2次,每次测试场。 为什么?只是想想...已经过去了我还,让我头疼
你不能做一些像这样
- (BOOL)textFieldShouldEndEditing:(UITextField *)txtField{
if(i_dont_know){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
return false;
}
return true;}
是那个UIAlertView中表演也试图辞职文本字段 的编辑和调用这个函数“textFieldShouldEndEditing:”...
所以我解决这个问题的方法是在界面中添加一个名为“shouldEndEditing”的成员变量,它是true por默认值。 而“textFieldShouldEndEditing:”可以是这样的一些。
- (BOOL)textFieldShouldEndEditing:(UITextField *)txtField{
if(shouldEndEditing == false)
{
shouldEndEditing = true;
return false;
}
if(i_dont_know){
shouldEndEditing = false;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
return false;
}
return true;}
好运...
另一种选择是让UIAlertView
假正确的验证和延迟校正部向后面的时间。事情是这样的:在每个TextView的
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
double delayInSeconds = 0.;
self.currentTextField.text = @"Something that won't trigger validation error";
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// do what you need here
});
}
你不能添加不同的代码,并检查在textFieldShouldEndEditing标签?或者我错过了这个观点?