UITextField valueDidChange-事件

发布于 2024-08-27 14:50:46 字数 239 浏览 6 评论 0原文

有没有办法捕获文本字段的“valueDidChange”事件? oO

我有一个带有 UITextField 的 modalView 。

当 UITextField 为空时,应禁用导航栏中的“完成”按钮 当输入文本时,它应该被启用。

目前,实现此目的的唯一方法是使用“textFieldShouldReturn”方法。 这可行,但对于可用性来说简直太糟糕了。

有没有办法在每次输入或删除字母时检查要求?

Is there a way to catch a "valueDidChange"-Event for a Textfield? oO

I've got a modalView with an UITextField.

When the UITextField is empty, the "Done"-Button in the NavigationBar should be disabled
and when there is Text entered, it should become enabled.

Right now the only way to accomplish this by using the "textFieldShouldReturn"-Method.
This works but is simply horrible for usability.

Isn't there a way to check the requirements every time a letter is being entered or removed?

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

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

发布评论

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

评论(3

时光与爱终年不遇 2024-09-03 14:50:46

如果有人关心:解决方案是使用 addTarget:selector:controlevent: 捕获控件事件“EditingChanged”:;)

If anybody cares: the solution is to catch the control event "EditingChanged" with addTarget:selector:controlevent: ;)

2024-09-03 14:50:46

我有一个带有 self.editButtonItemUITableViewController ,仅当 UITextField.text 中有文本时才启用它。这是一种确保用户在用某些内容填充此 UITextField 之前不会按“完成”的方法。我认为这正是您正在寻找的。我是这样做的:

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

    // Logs for seeing patterns, if you so please
    NSLog(@"replacementString length = %d",string.length);
    NSLog(@"textField.text = %@",textField.text);
    NSLog(@"range = (%d,%d)\n\n",range.location,range.length);

    if (string.length) {
        self.editButtonItem.enabled = YES;
    } else if (!string.length && textField.text.length == range.length) {
        self.editButtonItem.enabled = NO;
    }
    return YES;
}

希望有帮助

I have a UITableViewController with self.editButtonItem which is enabled only if the UITextField.text has text in it. It's a way of ensuring that the user doesn't press "Done" before he/she has filled this UITextField with something. I think this is exactly what you're looking for. Here's how I did it:

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

    // Logs for seeing patterns, if you so please
    NSLog(@"replacementString length = %d",string.length);
    NSLog(@"textField.text = %@",textField.text);
    NSLog(@"range = (%d,%d)\n\n",range.location,range.length);

    if (string.length) {
        self.editButtonItem.enabled = YES;
    } else if (!string.length && textField.text.length == range.length) {
        self.editButtonItem.enabled = NO;
    }
    return YES;
}

I hope that helps

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