7

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

如果用户在编辑项目后单击“完成”按钮并且保存/验证失败,则UIAlertView显示 a 并且用户保持在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;
}
4

4 回答 4

4

我认为您的问题来自在编辑 textField 并直接点击另一个时调用 textField 方法的顺序。

如果我没记错的话,应该是这样的(你在 A 上编辑并点击 B)

  • textFieldShouldBeginEditing对于字段 B
  • textFieldShouldEndEditing对于字段 A
  • textFieldDidEndEditing对于字段 A
  • textFieldDidBeginEditing对于字段 B

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

当我想在 textField 开始编辑时提出一个视图时,这对我来说也是一个问题。我找到的解决方案是创建一个布尔类变量,指示我当前是否正在从一个 textField 切换到另一个。我将它设置为TRUEintextFieldShouldBeginEditing和 to FALSEin textFieldDidBeginEditing。当您在 中时textFieldShouldEndEditing,如果设置为,TRUE则表示用户直接点击了另一个文本字段。然后你只需要找到正确的方法来进行一次测试(也许 shouldEndEditing 应该返回 false 或其他东西)。

于 2010-06-27T23:38:32.147 回答
1

您不能在每个文本视图中添加不同的标签并检查 textFieldShouldEndEditing 中的标签吗?还是我错过了重点?

于 2016-09-08T15:12:33.073 回答
1

另一种选择是让UIAlertView假货进行正确的验证并将更正部分推迟到以后的时间。像这样的东西:

- (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
    });
}
于 2015-07-29T14:55:40.323 回答
0

看起来对我来说每个测试字段都被调用了 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”的成员变量,它是默认的。而在“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;}

祝你好运...

于 2010-07-14T09:34:53.953 回答