Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField lose focus event

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.

like image 643
Alexi Groove Avatar asked Jul 15 '09 18:07

Alexi Groove


3 Answers

Try using delegate for the following method:

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

That worked for me.

like image 168
user3681049 Avatar answered Nov 16 '22 18:11

user3681049


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;
}
like image 36
Tim Bowen Avatar answered Nov 16 '22 18:11

Tim Bowen


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;
}
like image 4
tosan Avatar answered Nov 16 '22 18:11

tosan