Problem reading the latest value from UITextField before closing the view
I have a view that I'm displaying modally to get login information from a user. I have the following setup:
- The view controller is a
UITableViewController
- The table has three cells: username and password fields (both of type
UITextField
withinUITableViewCell
) and one simple cell as the login button. - Values are read from
UITextField
intextFieldDidEndEditing:
message - The text fields are identified by different tag values.
The problem
My problem is that when the focus is in a UITextField
and user touches the login button, the respective UITextField
's textFieldDidEndEditing:
message is sent after the didSelectRowAtIndexPath:
. Now the issue here is that I'm sending a message of new user credentials to my LoginViewControllerDelegate
in the didSelectRowAtIndexPath:
and at that time the text field's value is not read yet.
Some Ideas
I have some ideas how to fix this, but I have complications with each of them.
First, I could close the login view and the delegate is notified during the closing, but I want to give the delegate (one who owns this login view) full control and I think it should be the delegate's job to close the login view on successful login (the login view only reads the credentials, the delegate validates these).
Second, I could also read the username or password just before calling the delegate but then I'd have to look up the text fields. If the views are not visible, I think it is wrong to assume that the cells do exist. This is just a big if, but I wan't to make it right. Would it be ok to retain the UITextField
s? This way, howeve开发者_如何学Gor, I can't use some custom cell that would itself provide the textfield.
Basically I want the following:
- Need for (valid) user credentials is detected, login view is popped up
- User inputs the username and password and invokes done.
- The delegate validates these credentials. If they do not work, a message is shown and try step 2 again.
- Credentials are ok, so close the login view and continue.
After all tricks I finally figured that simply resigning the first responder right before invoking the delegate fixed this. In the table view controller I simply had to add a call:
[[self.view findFirstResonder] resignFirstResponder];
The findFirstResonder
is from a category and can be found here.
Try this instead of using third cell as login button. Use a simple button on login view. On that button's IBAction you can validate user input.
You can use a simple view with two textfields and one UIButton. After the button is pressed you can just remove the view from superview by [yourView removeFromSuperview]
. You can receive the data from the yourTextfield.text.
I guess this must do the trick
精彩评论