【问题标题】:Why isn't the UITextField clear button working in an iOS 5 WeeApp?为什么 UITextField 清除按钮在 iOS 5 WeeApp 中不起作用?
【发布时间】:2012-03-08 21:34:00
【问题描述】:

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

我的问题是textFieldShouldClear 方法永远不会在单击 UITextField 中的清除按钮时被调用。调用 shouldChangeCharactersInRange 和 textFieldShouldReturn 等其他接口方法没有问题。

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

【问题讨论】:

    标签: objective-c ios5 widget jailbreak notificationcenter


    【解决方案1】:

    当我在用户点击屏幕上的其他位置时关闭键盘时遇到了这个问题。我有一个手势识别器来寻找点击,当检测到点击时,它会在文本字段上调用 ​​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];
        }
    }
    

    【讨论】:

      【解决方案2】:

      确保文本字段的委托是自我:

      theTextField.delegate = self;
      

      我听说 UIActionSheet 吸收了 UITextFieldDelegate 协议,通知中心可能也会这样做......

      【讨论】:

      • textField 的delegate 已经设置为self 但没有任何区别。
      • 找到任何改变 UITextfield 委托的东西(除了将它设置为 self 的那个)并将其注释掉。然后看看它是否有效。我会使用写入文件的东西,因为 NSLog 在你的情况下很难。
      【解决方案3】:

      根据 Graham Perks 的回答,我将 resignFirstResponder 调用更改为:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-12
        • 1970-01-01
        • 2011-03-27
        • 1970-01-01
        • 2020-06-29
        • 2014-11-19
        • 1970-01-01
        相关资源
        最近更新 更多