GetFirstResponder in UITextView not working

For some reason, I am having problems creating the text box by the first responder.

I have a UITableView with two rows. Each line has a label and a UITextField. Text fields are marked with kLoginRowIndex = 0 and kPasswordRowIndex = 1. As you might have guessed, I use this to set the username and password.

If the user presses the back button while editing the text input field, I want the password text field to get focus. Unfortunately, the password text field does not accept focus. Here is my code:

- (BOOL)textFieldShouldReturn:(UITextField *)textField { NSLog(@"%s:(textField.tag:%d)", __FUNCTION__, textField.tag); [textField resignFirstResponder]; if(textField.tag == kLoginRowIndex) { UITableViewCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:kPasswordRowIndex inSection:0]]; UITextField *nextTextField = (UITextField *)[cell viewWithTag:kPasswordRowIndex]; NSLog(@"(nextTextField.tag:%d)", nextTextField.tag); NSLog(@"canBecomeFirstResponder returned %d", [nextTextField canBecomeFirstResponder]); NSLog(@"becomeFirstResponder returned %d", [nextTextField becomeFirstResponder]); } else { [self validate:textField]; } return NO; } 

This is the output of the log:

  - [SettingsViewController textFieldShouldReturn:] :( textField.tag: 0)
 (nextTextField.tag: 1)
 canBecomeFirstResponder returned 1
 becomeFirstResponder returned 0 

What I tried:

  • return YES instead of NO
  • removing the canBecomeFirstResponder call (which is for debugging purposes only)

Any hints appreciated!

+3
iphone uikit uitextfield
Dec 13 '09 at 12:44
source share
3 answers

After playing with the tmadsen sentence, I found an error. The error in this line is:

 UITableViewCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:k 

It returns a new cell, not the one on the screen. I replaced it with

 UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:kPasswordRowInde 

and now it works as expected.

On the side of the note, I found that 0 is the default value for the tag property, so it's probably not so smart to use it.

+10
Dec 13 '09 at 15:09
source share

0 is the default value for the tag property, so you probably want to use something other than 0, otherwise you will most likely return a supervisor when calling viewWithTag:

+3
Dec 13 '09 at 15:17
source share

It has been a while since I developed for the iPhone, and I never used the tag that you display in your code. But you can do what you want by creating the properties of the text fields of your class. If you do this and say that you name those loginTextField and passwordTextField properties, you can leave the following text screen as follows:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { if([self usernameTextField] == textField) { return [[self passwordTextField] becomeFirstResponder]; } else { // your validating code... } return NO; } 

But, as I said, some time has passed and I don’t know the tag you are talking about, so maybe this is a new best practice, but the above code should work

0
Dec 13 '09 at 13:26
source share



All Articles