textFieldDidEndEditing firing “too late”

风流意气都作罢 提交于 2019-12-08 02:49:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!