Question

I've created this in my .h:

@interface Class : UIViewController <UITextFieldDelegate

And then in my .m:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {    
if (textField == username) {
    [textField resignFirstResponder];

}else if (textField == password) {
    [textField resignFirstResponder];

}
return YES;
}

but when i try to run some code inside either the if for username or password it never gets run. I've tried to debug it, but when i tap on my text field it just opens the keyboard and it's not debugging. So it seems like textFieldShouldReturn never gets run, why?

Any help is much appreciated.

SOLUTION: Add: username.delegate = password.delegate = self; in your .m file

Was it helpful?

Solution

The purpose of textFieldShouldReturn: method is to determine if the text field should process the pressing of the return button on [virtual] keyboard.

I assume you are looking for textFieldShouldEndEditing: delegate method.

PS. I assume you have set password.delegate = username.delegate = self;

OTHER TIPS

This won't be called by tapping the text field, This will only get called when you hit the return key on the keyboard. Be sure you've set the delegate correctly.

This function is called when you hit return key on the keyboard and not when you touch the textfield

The function called when you touch the textfield is

- (void)textFieldDidBeginEditing:(UITextField *)textField
if (textField == username) {
    [textField resignFirstResponder];

}else if (textField == password) {
    [textField resignFirstResponder];

}

you can even try to set delegate for both username and password textfield from the xib/storyboard by connecting the outlet-delegate to the file owner. that may solve your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top