textFieldDidEndEditing 触发“太晚了”
我有一个多视图流程,用户正在输入数据,并且我将其保存到模型类属性中的每一步。
我使用 textFieldDidEndEditing
检查输入是否有效,如果有效,则保存输入的数据。
在视图上,我有一个 continueButtonClicked 事件,用于检查所有验证是否通过,如果通过则加载下一个视图。我没有在这里设置模型的属性,因为我认为我不必这样做,因为每个字段一次保存到模型 1 字段。但是,我注意到一些问题。
如果用户位于文本框内并单击“继续”按钮,则 continueButtonClicked 事件会在 textFieldDidEndEditing
之前触发。最终发生的情况是,在 textFieldDidEndEditing
中进行保存之前,下一个视图将填充“旧”模型。
我缺少什么?将所有属性设置在“继续”上是否正确?如果我为网络编程,我就会这样做,但它似乎不适合本机应用程序。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在活动文本字段/文本视图(或所有这些)上调用
resignFirstResponder
,这将触发textFieldDidEndEditing
。一般来说,对于此类文本编辑问题,另一种方法是滥用
-textField:shouldChangeCharactersInRange:...
委托方法,您可以在其中确定输入的每个字符的新值。仅当没有其他方法时才应恢复到此方法。You can call
resignFirstResponder
on the active textfield/textview (or on all of them), which will trigger thetextFieldDidEndEditing
.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.您可以尝试一下这个方法,看看是否能解决您的问题?
Can you try this instead and see if that solves your problem?