Question

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.

Was it helpful?

Solution

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

OTHER TIPS

Try using delegate for the following method:

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

That worked for me.

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

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

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.

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()
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top