为什么 UITextField 清除按钮在 iOS 5 WeeApp 中不起作用?

发布于 2025-01-06 18:45:01 字数 321 浏览 0 评论 0原文

我正在使用 iOSOpenDev 为通知中心制作一个 weeapp。我在 UIView 上有一个 UITextField ,并实现了 UITextFieldDelegate 协议。

我的问题是,单击 UITextField 中的清除按钮时永远不会调用 textFieldShouldClear 方法。其他接口方法(例如 shouldChangeCharactersInRange 和 textFieldShouldReturn)的调用不会出现问题。

有什么想法为什么接口方法永远不会被调用吗?

I'm making a weeapp for notification center using iOSOpenDev. I have a UITextField on a UIView and have implemented the UITextFieldDelegate protocol.

My problem is that the textFieldShouldClear method never gets called on clicking the clear button in the UITextField. Other interface methods such as shouldChangeCharactersInRange and textFieldShouldReturn are called without issue.

Any ideas why the interface method never gets called?

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

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

发布评论

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

评论(3

喜爱纠缠 2025-01-13 18:45:01

当用户点击屏幕上的其他位置时我关闭键盘时遇到了这个问题。我有一个手势识别器寻找点击,当检测到点击时,它会在文本字段上调用 ​​resignFirstResponder 。不幸的是,这打破了清除按钮。

我所做的是过滤点击以确保它们位于表格视图之外,由于必须手动触发按钮点击而稍微复杂:

// In: - (void)handleTap:(UITapGestureRecognizer *)sender {
// The user tapped outside a text field, drop the keyboard.
// Unfortunately this normally breaks the clear button, so we'll check that the
// tap is outside the table view (and therefore not on a clear button).
BOOL inButton = CGRectContainsPoint(self.signInButton.bounds, [sender locationInView:self.signInButton]);
BOOL inTable = CGRectContainsPoint(self.tableView.bounds, [sender locationInView:self.tableView]);
if (!inTable && !inButton ) {

    BOOL didEndEditing = [self.view endEditing:NO];

    // But... if we were editing a field (& therefore the keyboard is showing),
    // and if they tapped the sign in button, sign in. Not sure where the
    // onSignIn event is getting lost. The button does highlight. But the
    // call to endEditing seems to eat it.
    if (didEndEditing && inButton) {
        [self onSignIn:self.signInButton];
    }
}

I had this problem when I was dismissing the keyboard when the user tapped elsewhere on the screen. I had a gesture recognizer looking for taps, and when a tap was detected, it would call resignFirstResponder on the text field. Unfortunately, that breaks the clear button.

What I did was filter the taps to be sure they are outside the table view, slightly complicated by having to manually trigger button taps:

// In: - (void)handleTap:(UITapGestureRecognizer *)sender {
// The user tapped outside a text field, drop the keyboard.
// Unfortunately this normally breaks the clear button, so we'll check that the
// tap is outside the table view (and therefore not on a clear button).
BOOL inButton = CGRectContainsPoint(self.signInButton.bounds, [sender locationInView:self.signInButton]);
BOOL inTable = CGRectContainsPoint(self.tableView.bounds, [sender locationInView:self.tableView]);
if (!inTable && !inButton ) {

    BOOL didEndEditing = [self.view endEditing:NO];

    // But... if we were editing a field (& therefore the keyboard is showing),
    // and if they tapped the sign in button, sign in. Not sure where the
    // onSignIn event is getting lost. The button does highlight. But the
    // call to endEditing seems to eat it.
    if (didEndEditing && inButton) {
        [self onSignIn:self.signInButton];
    }
}
因为看清所以看轻 2025-01-13 18:45:01

确保文本字段的委托是 self:

theTextField.delegate = self;

我听说 UIActionSheet 吸收了 UITextFieldDelegate 协议,并且通知中心可能会做同样的事情......

Make sure that the textfield's delegate is self:

theTextField.delegate = self;

I have heard that UIActionSheet sucks up the UITextFieldDelegate protocol, and the notification center might do the same...

纸短情长 2025-01-13 18:45:01

按照 Graham Perks 的回答,我将 resignFirstResponder 调用更改为:

[self.focusInput performSelector: @selector(resignFirstResponder)
                      withObject: nil
                      afterDelay: 0.2f];

现在,键盘按预期自动隐藏在点击上,但清除按钮功能又回来了。

Following Graham Perks's answer, I changed my resignFirstResponder call to:

[self.focusInput performSelector: @selector(resignFirstResponder)
                      withObject: nil
                      afterDelay: 0.2f];

Now the keyboard is automatically hidden on taps as intended, but the clear button functionality is back.

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