【问题标题】:Trying to disable a button when not all UITextFields are populated in iOS在 iOS 中未填充所有 UITextField 时尝试禁用按钮
【发布时间】:2013-09-06 19:27:28
【问题描述】:

我正在开发一个应用程序,其中我有一个由自定义 UITableViewCell 组成的 UITableView。每个 UITableViewCell 都有一个 UITextField。我想要做的是在填充所有 UITextFields 时启用 UIButton,我现在可以做到这一点。我现在的挑战是在没有填充所有 UITextFields 时禁用这个按钮。这是我正在使用的代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    for (int i = 0; i < [self.table numberOfSections]; i++) {

        NSInteger rows =  [self.table numberOfRowsInSection:i];

        for (int row = 0; row < rows; row++) {

            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:i];
            SimpleTableCell *cell = (SimpleTableCell *)[self.table cellForRowAtIndexPath:indexPath];

            NSNumber *numKey = [NSNumber numberWithInt: cell.dataField.tag];
            NSString *text = cell.dataField.text;

            if ([[cell dataField] text] != nil && [[[cell dataField] text] length] > 0) {

                [_dict setObject:text forKey:numKey];

                NSArray *keys = [_dict allKeys];

                if ([keys count] == ([_dataName count] - 1)) { //where _dataName is the array that is populating my UITableView

                    [_doneButton setEnabled:YES];
                    [_doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:28]];

                }

            } else { //if there is a UITextField that is empty

                [_dict removeObjectForKey:numKey];

                NSArray *keys = [_dict allKeys];
                //below is the clause where I disable the button, after checking if there is a single UITextField that is not populated
                if ([keys count] < ([_tireName count] - 1)) {

                    [_doneButton setEnabled:NO];
                    [_doneButton.titleLabel setFont:[UIFont systemFontOfSize:28]];

                }

            }

        }

    }

    return YES;
}

只有在填充了所有字段时才能启用按钮,这正是我想要的,但不幸的是,它仅在有三个空 UITextFields 时才禁用按钮。我这样做的方法是:

当我浏览 UITableView 中的每个 UITextField 时,我会检查 UITextField 是否为空。如果它不为空,我将 UITextField 中的字符串值输入到 NSMutableDictionary 中,使用 UITextFields 标记值作为键。如果 UITextField 为空,那么我只需删除连接到该键的对象。从理论上讲,我的方法是有道理的,但它并没有像我希望的那样工作。谁能看到我做错了什么?

【问题讨论】:

  • tableView 有多少行?
  • 在我的示例中,我有八行。
  • 在什么情况下检查禁用按钮?
  • 我更新了我的代码来告诉你在哪里。

标签: ios uitableview uitextfield nsmutabledictionary


【解决方案1】:

当调用 textField:shouldChangeCharactersInRange:replacementString: 时,textField 的文本值尚未更改。此方法用于在文本实际更改之前确定是否应允许用户输入的文本。因此,当您检查文本是否为空时,正在编辑的文本字段会说它不是空的,即使它即将变为空。

换句话说,如果您正在编辑一个包含“A”的文本字段,并且用户按下退格键,当您在此方法中检查文本时,它会说它是“A”,而不是这是 ””。因此,当您清除其中一个文本字段的文本时,这将永远不会禁用该按钮。

当从第二个文本字段中删除文本时,它应该捕获对前一个文本字段的更改,但是您正在禁用按钮 if ([keys count] &lt; ([_tireName count] - 1)) 我相当确定这应该是 if([keys count] &lt; [_tireName count]) 同样我认为您也想要if([keys count] == [_dataName count]) 除非您实际上期望 _dataName 和 _tireName 总是比您的字典多 1 条记录。

因为当您的字典中的键少于 [_tireName count] -1 个键时,您将禁用该按钮,除非您有 2 个空文本字段,否则您似乎永远不会禁用该按钮。

一旦你清除了第二个文本字段,一旦你编辑了第三个,它就会认为是时候让你最终禁用该按钮了。

你可以尝试改变

 NSString *text = cell.dataField.text;

类似这样的东西

NSString* text;
if(textfield == cell.dataField) //if this cell's textfield is the field we are editing
{
    text = [cell.dataField.text stringByReplacingCharactersInRange:range withString:string];
}
else
{
    text = cell.dataField.text;
}

这将阻止您在进行验证时查看正在编辑的文本字段的旧值。 并将if([keys count] &lt; (_tireName count] -1)) 更改为if([keys count] &lt; [_tireName count])

您可能还有一种方法可以更改它,这样您就不需要遍历 tableview 中的所有行,但是如果您的 tableview 中只有 8 行,那么这种更改将收效甚微.

【讨论】:

  • 我真的要感谢您的解决方案。你给出的解释真的帮助我理解了这个问题,你的解决方案确实帮助了我。再次感谢。
猜你喜欢
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
相关资源
最近更新 更多