我正在研究具有多个UITextField
对象的视图。我的视图控制器用作UITextFieldDelegate
,并且我已经实现了(BOOL)textFieldShouldEndEditing:(UITextField *)textField
保存和验证正在显示的记录的方法。
如果用户在编辑项目后单击“完成”按钮并且保存/验证失败,则UIAlertView
显示 a 并且用户保持在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;
}