多次调用textFieldShouldEndEditing [英] textFieldShouldEndEditing called multiple times

查看:105
本文介绍了多次调用textFieldShouldEndEditing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理具有多个UITextField对象的视图.我的视图控制器用作UITextFieldDelegate,并且已经实现了(BOOL)textFieldShouldEndEditing:(UITextField *)textField方法来保存和验证正在显示的记录.

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.

如果用户在编辑项目后单击完成"按钮,并且保存/验证失败,则会显示UIAlertView,并且用户将停留在验证失败的UITextField上.

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.

我的问题是-当用户从UITextField单击将无法保存/验证到另一个UITextField上的失败时,然后多次调用(BOOL)textFieldShouldEndEditing:(UITextField *)textField方法,并弹出UIAlertView多次.

My problem is this -- when a user clicks from the UITextField that will fail save/validation onto another of the UITextFields, then the (BOOL)textFieldShouldEndEditing:(UITextField *)textField method is called multiple times, and the UIAlertView pops up multiple times.

为什么当用户单击键盘上的完成"时(BOOL)textFieldShouldEndEditing:(UITextField *)textField会被调用一次,而当用户单击另一个UITextField时为什么会被多次调用?

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?

这是我的代码:

- (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;
}

推荐答案

我认为您的问题来自编辑文本字段并直接点击另一个文本字段时调用textField方法的顺序.

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.

如果我没记错的话,应该是这样的(您正在A上进行编辑,然后点击B)

If I am not mistaken, it should be something like this (you are editing on A and tap on B)

    B字段的
  • textFieldShouldBeginEditing
  • textFieldShouldEndEditing用于字段A
  • textFieldDidEndEditing用于字段A
  • B字段的
  • textFieldDidBeginEditing
  • textFieldShouldBeginEditing for field B
  • textFieldShouldEndEditing for field A
  • textFieldDidEndEditing for field A
  • textFieldDidBeginEditing for field B

因此,当您使用textFieldShouldEndEditing方法时,文本字段B已经成为第一响应者.因此,当您使UIAlertView出现时,B会失去焦点,因此也会调用textFieldShouldEndEditing

So 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 calls textFieldShouldEndEditing too!

当我想在textField开始编辑时提出视图时,这对我来说也是一个问题.我发现的解决方案是创建一个布尔类变量,该变量指示我当前是否从一个textField切换到另一个. 我在textFieldShouldBeginEditing中将其设置为TRUE,在textFieldDidBeginEditing中将其设置为FALSE.在textFieldShouldEndEditing中时,如果将其设置为TRUE,则意味着用户直接点击另一个textField.然后,您只需要找到只进行一次测试的正确方法即可(也许应该EndEndinging应该返回false或其他内容).

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 in textFieldShouldBeginEditing and to FALSE in textFieldDidBeginEditing. When you're in textFieldShouldEndEditing, if it is set to TRUE 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).

这篇关于多次调用textFieldShouldEndEditing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆