如果多个 UITextField 都已填充,则启用 UIBarButtonItem

发布于 2024-08-28 21:21:08 字数 1259 浏览 1 评论 0原文

我在分组的 UITableView 中有 3 个 UITextField,并且正在尝试找出正确的逻辑,以便仅在没有 UITextField 为空时启用“保存”UIBarButtonItem。

我目前正在使用 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string UITextField 委托方法逐字符检测字段的更改,但它提供的结果不一致。

有什么想法吗?

编辑:这是我现在使用的代码。正如您所看到的,我已将文本字段放入一个数组中,以便我可以迭代它们。现在,直到我在第三个字段中输入第二个字符后,保存按钮才会启用。它还会交替启用/禁用,作为从字段中一一删除字符。

NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    BOOL allValid;

    if (newString.length)
    {
        // Cycle through array checking for completeness
        for (int i = 0; i < [textFieldArray count]; i++)
        {
            if ([[[textFieldArray objectAtIndex:i] text] length] > 0)
            {
                allValid = YES;
                NSLog(@"TextField #%i Validates.", i);
            }
            else
            {
                allValid = NO;
                NSLog(@"TextField #%i Does Not Validate.", i);
            }
        }
    }
    else
    {
        NSLog(@"Invalid");
        allValid = NO;
    }

    if (allValid)
        [saveButton setEnabled:YES];
    else
        [saveButton setEnabled:NO];

    return YES;

I have 3 UITextFields in a grouped UITableView and am trying to figure out the correct logic to only have my 'Save' UIBarButtonItem enabled when none of the UITextFields are empty.

I'm currently using the - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string UITextField delegate method to detect changes to the field character by character, but it is providing inconsistent results.

Any ideas?

Edit: Here is the code I'm now using. As you can see I've placed my text fields into an array so I can iterate through them. As it is now, the save button doesn't enable until I enter the 2nd character in the 3rd field. Also it alternates enabled/disabled as a remove characters one by one from the fields.

NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    BOOL allValid;

    if (newString.length)
    {
        // Cycle through array checking for completeness
        for (int i = 0; i < [textFieldArray count]; i++)
        {
            if ([[[textFieldArray objectAtIndex:i] text] length] > 0)
            {
                allValid = YES;
                NSLog(@"TextField #%i Validates.", i);
            }
            else
            {
                allValid = NO;
                NSLog(@"TextField #%i Does Not Validate.", i);
            }
        }
    }
    else
    {
        NSLog(@"Invalid");
        allValid = NO;
    }

    if (allValid)
        [saveButton setEnabled:YES];
    else
        [saveButton setEnabled:NO];

    return YES;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱果果 2024-09-04 21:21:08

您究竟如何使用该方法?我会这样做:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
  if (newString.length) {
    //If all the others are also non-empty, enable your button
  }
  return YES;
}

但在用户按下回车键后启用按钮可能更有意义,这更容易处理。只需在文本字段上使用 didEndEditingOnExit 或 didEndEditing 事件并检查它们是否为非空。

How exactly are you using the method? Here's how I would do it:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
  if (newString.length) {
    //If all the others are also non-empty, enable your button
  }
  return YES;
}

But it might make more sense for you to enable your button after the user presses the enter key, which is easier to deal with. Just use a didEndEditingOnExit or didEndEditing event on the text fields and check there to see if they're non-empty.

魂牵梦绕锁你心扉 2024-09-04 21:21:08

好吧,这就是我最终做到的。

我创建了 - (IBAction)validateFields:(id)sender 并将其连接到 UITextField 上的 Editing Changed 出口。看起来像这样。

- (IBAction)validateFields:(id)sender
{
    BOOL valid = YES;

    // On every press we're going to run through all the fields and get their length values. If any of them equal nil we will set our bool to NO.
    for (int i = 0; i < [textFieldArray count]; i++)
    {
        if (![[[textFieldArray objectAtIndex:i] text] length])
            valid = NO;
    }

    [saveButton setEnabled:valid];
}

我已经进行了相当不错的尝试,但无法让保存按钮在空文本字段的任何组合上启用,所以我想说这是正确的方法。

Ok here's how I finally did it.

I created - (IBAction)validateFields:(id)sender and connected it to the Editing Changed outlet on UITextField. It looks like this.

- (IBAction)validateFields:(id)sender
{
    BOOL valid = YES;

    // On every press we're going to run through all the fields and get their length values. If any of them equal nil we will set our bool to NO.
    for (int i = 0; i < [textFieldArray count]; i++)
    {
        if (![[[textFieldArray objectAtIndex:i] text] length])
            valid = NO;
    }

    [saveButton setEnabled:valid];
}

I've given it a pretty decent go and couldn't get the save button to enable on any combination of empty text fields so I'm going to say this is the way to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文