MissCoder87
MissCoder87

Reputation: 2669

Hiding Iphone Keyboard on loss of focus

Using what I've read online people say put a button in the background of the app then when clicked that button handles closing the keyboard. However reading some other people they say resign first repsonder for the text field - does that mean the textfield has taken control of the keyboard and wont release it until specifically told to?

My .h has:

@interface SearchItems : UIViewController {

    IBOutlet UIButton *btnKeyboard;
    IBOutlet UITextField *txtWhat;
    IBOutlet UITextField *txtWhere;

}

@property (nonatomic, retain) IBOutlet UIButton *btnKeyboard;
@property (nonatomic, retain) IBOutlet UITextField *txtWhat;
@property (nonatomic, retain) IBOutlet UITextField *txtWhere;

-(IBAction)closeKeyboard;

@end

The .m (cut down)

@synthesize btnKeyboard, txtWhat, txtWhere;

-(IBAction)closeKeyboard {

    [txtWhere resignFirstResponder];
    [txtWhat resignFirstResponder];

}

Which isn't working, any ideas?

Thanks

Tom

Upvotes: 0

Views: 629

Answers (1)

Dip Dhingani
Dip Dhingani

Reputation: 2499

Try using the delegate methods to resignResponder:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField
 {
    [textField resignFirstResponder];
    return YES;
}

Apart from this one you can also use this delegate method:

 - (void)textFieldDidEndEditing:(UITextField *)textField
 {
     [textField resignFirstResponder];
 }

These two methods mainly should be able to solve your keyboard hiding problem rather than using the background button technique.

Upvotes: 1

Related Questions