ios UITextField 失去焦点事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1133224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:10:50  来源:igfitidea点击:

UITextField lose focus event

iosobjective-cuitextfielduitextfielddelegate

提问by Alexi Groove

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.

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

The delegate for the UITextFieldis set to MyCustomUIViewvia IB and I also have 'Did End On Exit' and 'Editing Did End' events pointing to an IBActionmethod within MyCustomUIView.

对于委托UITextField设置为MyCustomUIView通过IB和我也有“真的结束退出时”和“编辑真的结束”指向一个事件IBAction中的方法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?

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

The delegate for the UITextFieldis set as MyCustomUIViewso I am receiving textFieldShouldReturnmessage to dismiss the keyboard when done.

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

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.

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

采纳答案by Tim Bowen

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

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

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

回答by user3681049

Try using delegate for the following method:

尝试将委托用于以下方法:

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

That worked for me.

那对我有用。

回答by tosan

The problem with the resignFirstRespondersolution solely is, that it can only be triggered by an explicit keyboardor UITextFieldevent. 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.

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

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

回答by Tom Dalling

You might have to subclass the UITextFieldand override resignFirstResponder. resignFirstResponderwill be called just as the text field is losing focus.

您可能必须子类化UITextField和 覆盖resignFirstResponderresignFirstResponder将在文本字段失去焦点时被调用。

回答by jenish

i think you have implemented UIKeyboardDidHideNotificationand in this event you

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

use code like

使用类似的代码

[theTextField resignFirstResponder];

remove this code.

删除此代码。

also same code write in textFieldShouldReturnmethod. this also lost focus.

同样的代码写入textFieldShouldReturn方法。这也失去了重点。

回答by lwdthe1

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 中苦苦挣扎的人。我们向 ViewController 的视图添加了一个手势识别器,这样当视图被点击时,我们就会关闭文本字段。重要的是不要取消对视图的后续点击。

Swift 2.3

斯威夫特 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()
    }