How can I hide the keyboard after the user presses ‘Done’, or taps the UITextField?
I have put this code in the AppDelegate:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
And I have linked the UITextField delegate in IB to the File’s Owner..
What am I doing wrong?
EDIT: Changin return NO -> return YES does nothing. I have IB open and clicking on file’s owner -> connections tab, there are multiple referencing outlets all pointing to the UITextfields. It still isn;t working..
UPDATE:
I added this to my function:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
int i = 5;
NSLog(@"%d", i);
return YES;
}
Nothing is getting printed to the console when I press return after setting focus to the textfield…So it’s not calling the function. Have I connected something up incorrectly? I have the return key acting as ‘Done’too, not sure if that makes a difference?
UPDATE:
I had my testfieldShouldReturn function in my AppDelegate instead of my viewcontroller. My mistake….Thanks for your help guys
Your code looks good. Can you make sure that you do have the delegate connection set up correctly? Try adding an NSLog to the delegate method to see if it is even being called.
…
Your question update points to this definitely being a connection issue. Try what Vince suggests and set the delegate explicitly in code. You’ll probably want to do this in the viewDidLoad method of your view controller to ensure that the textField has actually been loaded before you set its delegate. Post back about whether that works.