【问题标题】:Enabling done button after inserting one char in a textfield: textFieldDidEndEditing: or textFieldShouldBeginEditing: or?在文本字段中插入一个字符后启用完成按钮: textFieldDidEndEditing: 或 textFieldShouldBeginEditing: 或?
【发布时间】:2011-02-25 13:11:33
【问题描述】:

当用户在 uitextfield 中至少写入一个字符时,我想启用导航栏上的完成按钮(在模式视图中)。我试过了:

  • textFieldDidEndEditing:当前一个 uitextfield 退出第一响应者时启用按钮(因此当前 uitextfield 中的零字符)。
  • textFieldShouldBeginEditing:当文本字段成为第一响应者时调用。 还有其他方法吗?

[编辑]

解决办法是

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

但都没有

 [self.navigationItem.rightBarButtonItem setEnabled:YES];

[doneButton setEnabled:YES]; //doneButton is an IBOutlet tied to my Done UIBarButtonItem in IB

工作。

【问题讨论】:

标签: iphone ios uibarbuttonitem uitextfielddelegate


【解决方案1】:

正确的代码是;

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger length = editingTextField.text.length - range.length + string.length;
    if (length > 0) {
        yourButton.enabled = YES;
    } else { 
        yourButton.enabled = NO;
    }
    return YES;
}

编辑: 正如 MasterBeta 之前和 David Lari 之后正确指出的那样,该事件应该响应 Editing Changed。我正在使用 David Lari 的解决方案更新答案,因为这是标记为正确的解决方案。

- (IBAction)editingChanged:(UITextField *)textField
{
   //if text field is empty, disable the button
    _myButton.enabled = textField.text.length > 0;
}

【讨论】:

  • 干得好!很高兴看到您介绍了选择多个字符的场景。
  • 虽然这比最初接受的答案要好,但这个答案也不太正确。如果您打开了“清除”按钮(请参阅clearButtonMode),则在您清除字段时不会调用shouldChangeCharactersInRange。正如 MasterBeta 指出的那样,您应该回复“Editing Changed”(UIControlEventEditingChanged,如果以编程方式添加目标)。
  • 为了缩短代码,您可以将 if-else 替换为以下内容:yourButton.enabled = length > 0;
  • 在项目中按原样粘贴代码:-) 完美答案
【解决方案2】:

shouldChangeCharactersInRange 不会在用户按下文本字段控件的清除按钮时被调用。当文本字段为空时,您的按钮也应该被禁用。

IBAction 可以与文本字段控件的Editing Changed 事件连接。当用户键入或按下清除按钮时会调用它。

- (IBAction) editDidChanged: (id) sender {
    if (((UITextField*)sender).text.length > 0) {
        [yourButton setEnabled:YES];
    } else {
        [yourButton setEnabled:NO];
    }
}

【讨论】:

    【解决方案3】:

    @MasterBeta:几乎正确。按照他的指示将一个动作连接到Editing Changed,但是这段代码更简单,没有错别字:

    - (IBAction)editingChanged:(UITextField *)textField
    {
       //if text field is empty, disable the button
        _myButton.enabled = textField.text.length > 0;
    
    }
    

    【讨论】:

    • 或者,甚至可能是- (IBAction)editingChanged:(UITextField *)textField { _myButton.enabled = textField.text.length > 0; }
    【解决方案4】:

    其实在 Xcode 6.x 中标记 ON Auto-enable Return Key 就足够了

    【讨论】:

      【解决方案5】:

      This 答案似乎在所有情况下都有效。单一的字符,清晰和所有的变化。希望有人觉得这很有帮助。

      【讨论】:

        【解决方案6】:

        斯威夫特 2.2

        您可以将自定义方法“checkTextField()”分配给“myTextField”UITextField:

        myTextField.addTarget(self, action: #selector(self.checkTextField(_:)), forControlEvents: .EditingChanged);
        

        并在方法内切换完成按钮:

        func checkTextField(sender: UITextField) {
        
            doneButton.enabled = !sender.hasText();
        }
        

        不需要任何委托。

        【讨论】:

        • 请编辑更多信息。不建议使用纯代码和“试试这个”的答案,因为它们不包含可搜索的内容,也没有解释为什么有人应该“试试这个”。
        【解决方案7】:

        试试看:

        -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        int lenght = editingTextField.text.length - range.length + string.length;
        if (lenght > 0) {
            yourButton.enabled = YES;
        } else { 
            yourButton.enabled = NO;
        }
        return YES;
        

        }

        当事实上下面的“w4nderlust”存在更好的解决方案时,这个答案被标记为正确。这个答案是他们的,让他们承担责任!

        【讨论】:

        • 这很奇怪......现在 doneButton.enabled = YES 有效。当文本长度为 0 时,我还添加了 doneButton.enabled = NO,它可以工作,但是当字符为两个时,按钮也被禁用......所以 0 字符按钮被禁用,1/3/4/5/6 ... 启用字符按钮,禁用 2 个字符按钮。我不明白为什么会出现这种行为。
        • @fran 我不明白“,但是当字符为两个时,按钮也被禁用...所以 0 字符按钮禁用,1/3/4/5/6...字符-启用按钮,禁用 2 个字符按钮。我不明白为什么会出现这种行为” - 向我展示你的 shouldChangeCharactersInRange 委托方法的实现。
        • - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSLog(@"%@ %d", textField.text, textField.text 。长度); if (textField == password) { if (textField.text.length+1 > 0) { doneButton.enabled = YES; } else { doneButton.enabled = NO; } } 返回是; }
        • 我解决了: if (textField.text.length >= 0) { doneButton.enabled = YES; } if (string.length == 0 && textField.text.length == 1) { doneButton.enabled = NO; }
        • 查看 w4nderlust 答案。它适用于所有情况,因为选择多个字符时会考虑“NSRange”。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-19
        • 2020-03-14
        • 1970-01-01
        • 1970-01-01
        • 2013-10-09
        相关资源
        最近更新 更多