【问题标题】:UITextField lose focus eventUITextField 失去焦点事件
【发布时间】:2009-07-15 18:48:50
【问题描述】:

我在 MyCustomUIView 类中有一个 UITextField,当 UITextField 失去焦点时,我想隐藏该字段并在适当的位置显示其他内容。

UITextField 的委托通过 IB 设置为 MyCustomUIView,我还有 'Did End On Exit' 和 'Editing Did End' 事件指向 MyCustomUIView 中的 IBAction 方法。

@interface MyCustomUIView : UIView { 

IBOutlet UITextField    *myTextField;

}

-(IBAction)textFieldLostFocus:(UITextField *)textField;

@end

但是,当 UITextField 失去焦点时,这些事件似乎都不会被触发。您如何捕获/查找此事件?

UITextField 的代表设置为MyCustomUIView,所以我收到textFieldShouldReturn 消息,在完成后关闭键盘。

但我也感兴趣的是确定用户何时按下屏幕上的其他区域(比如另一个控件或只是空白区域)并且文本字段失去焦点。

【问题讨论】:

    标签: ios objective-c uitextfield uitextfielddelegate


    【解决方案1】:

    尝试以下方法使用委托:

    - (BOOL) textFieldShouldEndEditing:(UITextField *)textField {
        NSLog(@"Lost Focus for content: %@", textField.text);
        return YES;
    }
    

    这对我有用。

    【讨论】:

      【解决方案2】:

      我相信您需要将您的视图指定为 UITextField 委托,如下所示:

      @interface MyCustomUIView : UIView <UITextFieldDelegate> { 
      

      作为一个额外的好处,当他们按下“完成”或返回按钮时,这是让键盘消失的方式,具体取决于您设置该属性的方式:

      - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
        //This line dismisses the keyboard.       
        [theTextField resignFirstResponder];
        //Your view manipulation here if you moved the view up due to the keyboard etc.       
        return YES;
      }
      

      【讨论】:

        【解决方案3】:

        resignFirstResponder 解决方案的问题在于,它只能由显式 keyboardUITextField 事件触发。 如果在文本字段之外的某个地方被点击,我也在寻找一个“失去焦点事件”来隐藏键盘。 我遇到的唯一接近且实用的“解决方案”是,禁用其他视图的交互,直到用户完成编辑(在键盘上点击完成/返回),但仍然能够在文本字段之间跳转以进行更正而无需每次都需要滑出和滑入键盘。

        以下 sn-p 可能对想要做同样事情的人有用:

        // disable all views but textfields
        // assign this action to all textfields in IB for the event "Editing Did Begin"
        -(IBAction) lockKeyboard : (id) sender {
        
            for(UIView *v in [(UIView*)sender superview].subviews)
                if (![v isKindOfClass:[UITextField class]]) v.userInteractionEnabled = NO;
        }
        
        // reenable interactions
        // assign this action to all textfields in IB for the event "Did End On Exit"
        -(IBAction) disMissKeyboard : (id) sender {
        
            [(UIResponder*)sender resignFirstResponder]; // hide keyboard
        
            for(UIView *v in [(UIView*)sender superview].subviews)
                v.userInteractionEnabled = YES;
        }
        

        【讨论】:

          【解决方案4】:

          您可能必须继承 UITextField 并覆盖 resignFirstResponderresignFirstResponder 将在文本字段失去焦点时被调用。

          【讨论】:

            【解决方案5】:

            我认为你已经实现了UIKeyboardDidHideNotification,在这种情况下你

            使用类似的代码

            [theTextField resignFirstResponder];
            

            删除此代码。

            同样的代码写在textFieldShouldReturn 方法中。这也失去了焦点。

            【讨论】:

              【解决方案6】:

              对于那些在 Swift 中苦苦挣扎的人。我们将手势识别器添加到 ViewController 的视图中,以便在点击视图时关闭文本字段。重要的是不要取消对视图的后续点击。

              斯威夫特 2.3

                  override func viewDidLoad() {
                      //.....
              
                      let viewTapGestureRec = UITapGestureRecognizer(target: self, action: #selector(handleViewTap(_:)))
                      //this line is important
                      viewTapGestureRec.cancelsTouchesInView = false
                      self.view.addGestureRecognizer(viewTapGestureRec)
              
                       //.....
                  }
              
                  func handleViewTap(recognizer: UIGestureRecognizer) {
                      myTextField.resignFirstResponder()
                  }
              

              【讨论】:

                【解决方案7】:

                Swift 中,您需要将UITextFieldDelegate 实现到您的ViewController 并从delegate 实现方法 textFieldDidEndEditing

                class ViewController: UIViewController, UITextFieldDelegate {
                    func textFieldDidEndEditing(_ textField: UITextField) {
                        // will be called when the text field loses their focus
                    }
                }
                

                那你需要给你的textField分配delegate

                textField.delegate = self
                

                【讨论】:

                  猜你喜欢
                  • 2011-09-16
                  • 2023-03-19
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-08-22
                  • 1970-01-01
                  相关资源
                  最近更新 更多