【发布时间】:2010-06-25 21:04:46
【问题描述】:
我正在处理具有多个 UITextField 对象的视图。我的视图控制器用作UITextFieldDelegate,我已经实现了(BOOL)textFieldShouldEndEditing:(UITextField *)textField 方法来保存和验证正在显示的记录。
如果用户在编辑项目后单击“完成”按钮并且保存/验证失败,则会显示UIAlertView,并且用户将继续使用验证失败的UITextField。
我的问题是——当用户从 UITextField 中单击将无法保存/验证到另一个 UITextFields 时,(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;
}
【问题讨论】:
标签: ios iphone objective-c uitextfield uitextfielddelegate