如何使用 iPhone 导航控制器在视图之间共享数据
我有一个视图,它从数组中获取数据并在表视图中显示数据。该视图有一个导航控制器,其中有一个按钮。该按钮旨在将您带到另一个视图以进行高级搜索。假设在这个新视图中我有一个选择器,当用户从选择器中选择一个值并单击导航栏中的后退按钮时,我想获取用户选择的值。这样做的最佳做法是什么?如何将选定的值从一项活动发送到前一项活动?
提前致谢。
i have a view which is fetching data from an Array and presenting the data in a tableview. This view has a navigation controller with a button in it. The button is meant to take you to another view for advanced searching. Let's say that in this new view i have a picker, when the user selects a value from the picker and clicks the back button in the navigation bar i want to get the value that the user selected. What is the best practice to do that? How can i send the selected value from one activity to the previous one?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在这里需要的是根据 模型进行一些推理-View-Controller 设计模式。
视图应该通过模型获取数据。因此,在高级搜索视图中,当用户选择某个值时,该值将存储在模型中。
当您返回时,第一个视图通过从模型中读取当前搜索值来重绘自身。
还有其他可能性,例如让搜索视图控制器拥有指向第一个视图的指针,并在搜索值更改时向其发送消息,但这不是很模块化并且非常脆弱。
What you need here is reasoning a bit in terms of the Model-View-Controller design pattern.
Views should get their data through the model. So in your advanced search view, when the user selects some value, this value is stored in the model.
When you go back, the first view redraw itself by reading the current search value from the model.
There are other possibilities, like having the search view controller own a pointer to the first view and sending a message to it when the search value changes, but this is not very modular and is pretty fragile.
使用委托。编写您自己的协议,例如“PickerViewDelegate”。然后在您的“主视图”(具有表格视图)中实现此协议。在 PickerView 中只需调用 [delegate SomethingPicked:something]。
我不确定,搜索值是一个模型实体。
Use delegation. Write your own protocol like "PickerViewDelegate". then implement this protocol in your "main view" (which has table view). in PickerView just invoke [delegate somethingPicked:something].
I'm not sure, that search value is a model entity.