在文本字段中插入一个字符后启用完成按钮:textFieldDidEndEditing:或textFieldShouldBeginEditing:或?

发布于 2024-10-19 17:53:13 字数 673 浏览 8 评论 0原文

当用户在 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

可行。

I would like to enable the done button on the navbar (in a modal view) when the user writes at least a char in a uitextfield. I tried:

  • textFieldDidEndEditing: enables the button when the previous uitextfield resigns first responder (so with the zero chars in the current uitextfield).
  • textFieldShouldBeginEditing: is called when the textfield becomes the first responder.
    Is there another way to do this?

[EDIT]

The solution could be

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

but neither

 [self.navigationItem.rightBarButtonItem setEnabled:YES];

or

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

work.

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

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

发布评论

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

评论(7

旧城烟雨 2024-10-26 17:53:13

正确的代码是;

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

The correct code is;

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

Edit:
As correctly pointed out by MasterBeta before and David Lari later, the event should respond to Editing Changed. I'm updating the answer with David Lari's solution as this was the one marked as correct.

- (IBAction)editingChanged:(UITextField *)textField
{
   //if text field is empty, disable the button
    _myButton.enabled = textField.text.length > 0;
}
晨与橙与城 2024-10-26 17:53:13

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

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

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

But shouldChangeCharactersInRange won't be called when user press clear button of text field control. And your button should also be disabled when text field is empty.

A IBAction can be connected with Editing Changed event of text field control. And it will be called when users type or press clear button.

- (IBAction) editDidChanged: (id) sender {
    if (((UITextField*)sender).text.length > 0) {
        [yourButton setEnabled:YES];
    } else {
        [yourButton setEnabled:NO];
    }
}
情释 2024-10-26 17:53:13

@MasterBeta:几乎是正确的。按照他的说明将操作连接到编辑已更改,但此代码更简单并且没有拼写错误:

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

}

@MasterBeta: Almost correct. Follow his instructions to connect an action to Editing Changed, but this code is simpler and has no typos:

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

}
双手揣兜 2024-10-26 17:53:13

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

Actually, in Xcode 6.x is sufficient to flag in ON Auto-enable Return Key

风筝在阴天搁浅。 2024-10-26 17:53:13

这个答案似乎适用于所有场景。单一字符,清晰且变化万千。希望有人觉得这有帮助。

This answer seems to be working in all scenarios. Single character, clear and all changes. Hope someone finds this helpful.

轻拂→两袖风尘 2024-10-26 17:53:13

Swift 2.2

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

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

并将方法内的完成按钮切换为:

func checkTextField(sender: UITextField) {

    doneButton.enabled = !sender.hasText();
}

不需要任何委托。

Swift 2.2

You can assign custom method "checkTextField()" to "myTextField" UITextField as:

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

and toggle the done button inside the method as:

func checkTextField(sender: UITextField) {

    doneButton.enabled = !sender.hasText();
}

No need of any delegate.

ˇ宁静的妩媚 2024-10-26 17:53:13

尝试并继续:

-(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”存在更好的解决方案。这个答案是他们的,功劳就归他们吧!

try and go with:

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

}

This answer was marked correct when infact a better solution existed by 'w4nderlust' below. This answer is theirs, let them take the credit!

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