textFieldShouldEndEditing 被调用多次
我正在处理一个具有多个 UITextField 对象的视图。我的视图控制器充当 UITextFieldDelegate
,并且我已实现 (BOOL)textFieldShouldEndEditing:(UITextField *)textField
方法来保存和验证正在显示的记录。
如果用户在编辑项目后单击“完成”按钮并且保存/验证失败,则会显示 UIAlertView
并将用户保留在失败的 UITextField
上验证。
我的问题是这样的——当用户点击 UITextField
时,将无法保存/验证到另一个 UITextField
,然后 (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;
}
I am working on a view that has multiple UITextField
objects. My view controller serves as the UITextFieldDelegate
, and I've implemented the (BOOL)textFieldShouldEndEditing:(UITextField *)textField
method to save and validate the record being displayed.
If the user clicks on the "Done" button after editing an item and the save/validate fails, then a UIAlertView
is displayed and the user is kept on the UITextField
that fails validation.
My problem is this -- when a user clicks from the UITextField
that will fail save/validation onto another of the UITextField
s, then the (BOOL)textFieldShouldEndEditing:(UITextField *)textField
method is called multiple times, and the UIAlertView
pops up multiple times.
Why is (BOOL)textFieldShouldEndEditing:(UITextField *)textField
called once when the user clicks "Done" on the keyboard, but called multiple times when the user clicks onto another UITextField
?
Here is my code:
- (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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您的问题来自于当您编辑文本字段并直接点击另一个文本字段时调用文本字段方法的顺序。
如果我没记错的话,应该是这样的(您正在 A 上编辑并点击 B)
textFieldShouldBeginEditing
for field BtextFieldShouldEndEditing
for field AtextFieldDidEndEditing< /code> 对于字段 A
textFieldDidBeginEditing
对于字段 B因此,当您使用
textFieldShouldEndEditing
方法时,文本字段 B 已经成为第一响应者。因此,当您使 UIAlertView 出现时,B 会失去焦点,从而也会调用textFieldShouldEndEditing
!当我想在文本字段开始编辑时提出视图时,这对我来说也是一个问题。我找到的解决方案是创建一个布尔类变量,指示我当前是否从一个文本字段切换到另一个文本字段。
我在
textFieldShouldBeginEditing
中将其设置为TRUE
,在textFieldDidBeginEditing
中将其设置为FALSE
。当您处于textFieldShouldEndEditing
中时,如果将其设置为TRUE
,则意味着用户直接点击了另一个文本字段。然后你只需要找到正确的方法来只进行一次测试(也许 shouldEndEditing 应该返回 false 或其他东西)。I think your problem comes from the order in which textField methods are called when you are editing a textField and directly tap onto another.
If I am not mistaken, it should be something like this (you are editing on A and tap on B)
textFieldShouldBeginEditing
for field BtextFieldShouldEndEditing
for field AtextFieldDidEndEditing
for field AtextFieldDidBeginEditing
for field BSo when you are in
textFieldShouldEndEditing
method, textfield B already has become the first responder. So when you make the UIAlertView appear, B loses focus and thus callstextFieldShouldEndEditing
too!This also has been a problem for me when I wanted to raise a view when a textField started editing. The solution I found was to create a boolean class variable indicating whether or not I am currently switching from one textField to another.
I set it to
TRUE
intextFieldShouldBeginEditing
and toFALSE
intextFieldDidBeginEditing
. When you're intextFieldShouldEndEditing
, if it is set toTRUE
it means the user directly tapped on another textField. Then you just have to find the right way to make your tests only once (maybe shouldEndEditing should return false or something).另一种选择是让 UIAlertView 伪造正确的验证并将更正部分推迟到稍后的时间。像这样的东西:
Another option is to let the
UIAlertView
fake a correct validation and defer the correcting part to a latter time. Something like this :您不能在每个文本视图中添加不同的标签并检查textFieldShouldEndEditing中的标签吗?还是我错过了重点?
Couldn't you add different tags in each textview and check the tag in the textFieldShouldEndEditing? Or have I missed the point?
每个测试领域对我来说似乎被叫了 2 次。
为什么?只是想想...也传递给了我,让我头疼
你不能做这样的事情
UIAlertView 显示也试图放弃文本字段的编辑
并调用这个函数“textFieldShouldEndEditing:”...
所以我解决这个问题的方法是在界面声明中添加一个名为“shouldEndEditing”的成员变量,默认为true。
在“textFieldShouldEndEditing:”之后可以是这样的。
祝你好运...
Looks right for me to be called 2 times for each test field.
Why? just think about... had passed to me also and get me headache
You can NOT do some like this
Is that the UIAlertView show is also trying to resign the editing of text field
and calling this funcion "textFieldShouldEndEditing:"...
So my way to solve this was to add a member variable called "shouldEndEditing" in the interface delaration, who is true por default.
And after the "textFieldShouldEndEditing:" can be some like this.
Good luck...