Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textFieldShouldReturn is not called

I want to make the keyboard disappear when the user clicks the "return" button, I was told to use

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

But nothing happens when I click the "return" button, the method isn't even being called. I am doing this in

@interface gameOverMenu : UIView

not in the ViewController. I also don't use interface builder. What should I do?

like image 426
Alexander Avatar asked Apr 23 '12 19:04

Alexander


2 Answers

You need to make sure you implement the UITextFieldDelegate and set your UITextField delegate to self. In your .h file:

@interface gameOverMenu : UIView <UITextFieldDelegate>

And somewhere in your .m file (viewDidLoad: maybe):

self.yourTextField.delegate = self;

Now your -(BOOL)textFieldShouldReturn:(UITextField *)textField method should be called.

like image 109
nicholjs Avatar answered Nov 02 '22 19:11

nicholjs


Make sure that you have set the parent class (whatever it is) as a UITextFieldDelegate

like image 2
LJ Wilson Avatar answered Nov 02 '22 18:11

LJ Wilson