UITextField 失去焦点事件

发布于 2024-07-27 00:24:54 字数 671 浏览 3 评论 0原文

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

UITextField 的委托通过 IB 设置为 MyCustomUIView,并且我还有指向 IBAction< 的“退出时结束”和“编辑结束”事件MyCustomUIView 中的 /code> 方法。

@interface MyCustomUIView : UIView { 

IBOutlet UITextField    *myTextField;

}

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

@end

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

UITextField 的委托设置为 MyCustomUIView,因此我收到 textFieldShouldReturn 消息以在完成后关闭键盘。

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

I have an UITextField in a MyCustomUIView class and when the UITextField loses focus, I'd like to hide the field and show something else in place.

The delegate for the UITextField is set to MyCustomUIView via IB and I also have 'Did End On Exit' and 'Editing Did End' events pointing to an IBAction method within MyCustomUIView.

@interface MyCustomUIView : UIView { 

IBOutlet UITextField    *myTextField;

}

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

@end

However, neither of these events seem to get fired when the UITextField loses focus. How do you trap/look for this event?

The delegate for the UITextField is set as MyCustomUIView so I am receiving textFieldShouldReturn message to dismiss the keyboard when done.

But what I'm also interested in is figuring when the user presses some other area on the screen (say another control or just blank area) and the text field has lost focus.

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

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

发布评论

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

评论(7

迷荒 2024-08-03 00:24:54

尝试使用委托执行以下方法:

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

这对我有用。

Try using delegate for the following method:

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

That worked for me.

秋叶绚丽 2024-08-03 00:24:54

我相信您需要将您的视图指定为 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;
}

I believe you need to designate your view as a UITextField delegate like so:

@interface MyCustomUIView : UIView <UITextFieldDelegate> { 

As an added bonus, this is how you get the keyboard to go away when they press the "done" or return buttons, depending on how you have set that property:

- (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;
}
清眉祭 2024-08-03 00:24:54

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

以下代码片段可能对想要做同样事情的人有用:

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

The problem with the resignFirstResponder solution solely is, that it can only be triggered by an explicit keyboard or UITextField event.
I was also looking for a "lost focus event" to hide the keyboard, if somewhere outside the textfield was tapped on.
The only close and practical "solution" I came across is, to disable interactions for other views until the user is finished with the editing (hitting done/return on the keyboard) but still to be able to jump between the textfields to make corrections without the need of sliding out and in the keyboard everytime.

The following snippet maybe useful to someone, who wants to do the same thing:

// 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;
}
冷默言语 2024-08-03 00:24:54

您可能必须对 UITextField 进行子类化并重写 resignFirstResponderresignFirstResponder 将在文本字段失去焦点时被调用。

You might have to subclass the UITextField and override resignFirstResponder. resignFirstResponder will be called just as the text field is losing focus.

很酷又爱笑 2024-08-03 00:24:54

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

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

然后您需要指定 textFielddelegate

textField.delegate = self

In Swift, you need to implement the UITextFieldDelegate to your ViewController and implement the method textFieldDidEndEditing from the delegate:

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

Then you need to asign the delegate of your textField:

textField.delegate = self
吖咩 2024-08-03 00:24:54

我认为您已经实现了 UIKeyboardDidHideNotification 并且在这种情况下您

使用类似

[theTextField resignFirstResponder];

删除此代码的代码。

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

i think you have implemented UIKeyboardDidHideNotification and in this event you

use code like

[theTextField resignFirstResponder];

remove this code.

also same code write in textFieldShouldReturn method. this also lost focus.

ゝ杯具 2024-08-03 00:24:54

对于那些在 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()
    }

For those struggling with this in Swift. We add a gesture recognizer to the ViewController's view so that when the view is tapped we dismiss the textfield. It is important to not cancel the subsequent clicks on the view.

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