跨视图控制器调用方法 [英] calling methods across view controllers

查看:43
本文介绍了跨视图控制器调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于在UIViewController之间实现信息传递的最佳方法.

My question is about the best way to implement passing of information between UIViewControllers.

如果我有一个带有2个ViewController的应用程序,例如,某个用户在ViewControllerA中选择了一个项目,然后应在ViewControllerB中显示该项目和更多详细信息.

If I have an application with 2 ViewControllers and for example a user selects an item in ViewControllerA which should then show the item and more details in ViewControllerB.

实现此目标的最佳方法是什么?通过appdelegate?或通过将对ViewControllerA的引用传递到ViewControllerB中?感谢任何帮助或最佳方法示例.

What would be the best way to implement this? via the appdelegate? or by passing a reference to ViewControllerA into ViewControllerB? Appreciate any help or examples of the best way to do this.

推荐答案

ViewControllerA(VCA)将维护对ViewControllerB(VCB)的引用.VCB会将对所选对象的引用保持为ivar.当用户在VCA中选择一个对象时,VCA实例化VCB(如果尚未实例化),将VCB的 selectedObject 属性设置为该对象,然后推送VCB.VCB从分配给其selectedObject属性的对象中读取信息,以将其信息绘制到视图中.

ViewControllerA (VCA) would maintain a reference to ViewControllerB (VCB). VCB would maintain a reference to the selected object as an ivar. When the user chooses an object in VCA, VCA instantiates VCB (if not already instantiated), sets VCB's selectedObject property to that object, and then pushes VCB. VCB reads from the object assigned to its selectedObject property to draw its information into the view.

在VCA中,对于用户可以选择的每个项目",都需要一个支持该项目的基础对象实例.例如,如果用户从蔬菜列表中进行选择,则UITableView可能由NSArray的Vegetable对象支持.

In VCA, for every one of the "items" that the user can select, there needs to be an underlying object instance backing that item. For example, a UITableView might be backed by an NSArray of Vegetable objects if the user is selecting from a list of vegetables.

通常,请尽量减少控制器之间的数据共享.让他们转而引用模型对象来获取数据.

In general, try to keep data sharing between controllers to a minimum. Have them refer to model objects instead to get their data.

这篇关于跨视图控制器调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆