问题
I have a multiview process the user is entering in data and I'm saving it to the model class properties each step along the way.
I use textFieldDidEndEditing
to check if the input is valid, and if so, saves the entered data.
On the view I have a continueButtonClicked event that checks to see if all the validations pass and if so loads the next view. I do NOT set the properties of the model here, because I think I shouldn't have to since each field is saved to the model 1 field at a time. However, I noticed some issues.
If the user is inside of a textbox and clicks the "Continue" button, the continueButtonClicked event fires BEFORE the textFieldDidEndEditing
. What ends up happening is that the next view is populated with the "old" model prior to the save happening in textFieldDidEndEditing
.
What am I missing? Is it proper to set all the properties on the Continue? That's how I would do it if I were programming for the web, but it doesn't seem right for a native app.
回答1:
Can you try this instead and see if that solves your problem?
textFieldShouldEndEditing:
回答2:
You can call resignFirstResponder
on the active textfield/textview (or on all of them), which will trigger the textFieldDidEndEditing
.
In general, for such text editing issues, another approach is to abuse the -textField:shouldChangeCharactersInRange:...
delegate method, in which you can determine the new value for every character entered. You should only revert to this method if there is no other way.
来源:https://stackoverflow.com/questions/9322005/textfielddidendediting-firing-too-late