iPhone:如何在选项卡栏应用程序中的多个视图控制器之间传递数据
我有以下问题:
我构建了一个带有 4 个选项卡的选项卡栏应用程序。我想将对象/变量从第一个选项卡控制器传递到第三个选项卡控制器,并使用相应的对象初始化该控制器。
我已经做了一些研究。与干净模型方法相对应的最佳方法是在被调用的视图控制器上调用一些 initWithObject: 方法。 我怎样才能实现这个目标?如何在调用者控制器中调用接收者控制器的 init
方法?你能给我一些代码示例吗?
编辑: 要在多个视图/类等之间传递数据,只需创建某种数据类,该数据类保存在多个类之间共享的数据。欲了解更多信息,请点击链接: 单例
I have following problem:
I have built a tabbar application with 4 tabs. I want to pass a object/variable from the first tab controller to the third one and initialize this controller with the corresponding object.
I've already done some research. The best way, corresponding to a clean model approach, would be to call some initWithObject: method on the called viewcontroller.
How can I achieve this? How can I call the init
method of the receivercontroller within the callercontroller? Can you give me some code example?
Edit:
To pass data between several views/classes etc simply create some Kind of data class which holds the data beeing shared between several classes. For more information follow the link:
Singleton
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要一个存储应用程序数据的数据模型对象。
数据模型是可从应用程序中的任何位置访问的定制的独立对象。数据模型对象对任何视图或视图控制器一无所知。它只存储数据以及数据之间的逻辑关系。
当应用程序的不同部分需要写入或读取数据时,它们会写入和读取数据模型。在您的情况下,view1 会在卸载时将其数据保存到数据模型中,然后 view2 会在加载时从数据模型中读取该数据(反之亦然)。
在正确设计的应用程序中,任何两个视图控制器都不应有权访问另一个控制器的内部数据。 (视图控制器需要知道另一个控制器存在的唯一原因是它是否必须触发另一个控制器的加载。)
创建数据模型的快速而肮脏的方法是将属性添加到应用程序委托,然后使用以下方法从视图控制器调用应用程序委托:
这适用于小型项目,但随着数据变得复杂,您应该为数据模型创建一个专用类。
编辑:
为了澄清您的具体情况,您可以在接收器 viewController 变为活动状态时添加对数据模型的调用。
将数据放入 init 方法或 viewDidLoad 中将不起作用,因为在 UITabBar 中用户可以来回切换,而无需卸载视图或重新初始化视图控制器。
检索更改数据的最佳位置是在 viewWillAppear 控制器方法中。这样,每次用户切换到该选项卡时,数据都会更新。
You need a data model object that stores the data for application.
A data model is a customized, standalone object accessible from anywhere in the application. The data model object knows nothing about any views or view controllers. It just stores data and the logical relationships between that data.
When different parts of the app need to write or read data, they write and read to the data model. In your case, view1 would save its data to the data model when it unloads and then view2 would read that data from the data model when it loads (or vice versa.)
In a properly designed app, no two view controllers should have access to the internal data of another controller. (The only reason a view controllers needs to know of the existence of another controller is if it has to trigger the loading of that other controller.)
The quick and dirty way to create a data model is to add attributes to the app delegate and then call the app delegate from the view controllers using:
This will work for small project but as your data grows complex, you should create a dedicated class for your data model.
Edit:
To clarify for your specific case, you would add the call to the data model when the receiver viewController becomes active.
Placing the data in an init method or a
viewDidLoad
won't work because in aUITabBar
the users can switch back and forth without unloading the view or reinitializing the view controller.The best place to retrieve changing data is in the
viewWillAppear
controller method. That way the data will be updated every time the user switches to that tab.您可能需要考虑
NSNotificationCenter
(参考);您向应用程序通知中心注册一个视图控制器,并在做出选择时发送通知。当收到通知时,另一个视图控制器会相应地更新自身。You might want to consider
NSNotificationCenter
(Reference); you register the one viewcontroller with the application notification center, and send a notification when a selection is made. When the notification is received, the other viewcontroller updates itself accordingly.我不认为这是最佳实践(还要检查语法),但是我已经摆脱了:
在 .h
和 .m 中
,然后我只是从方便的地方分配引用,例如应用程序委托或实例化视图控制器的任何地方。
然后视图控制器可以获得对另一个视图控制器的引用。
我将 @class secondaryviewcontroller 添加到第一个视图控制器的 .h 文件中,并将 #imports“secondviewcontroller.h” 放入第一个视图控制器的 .m 文件中。这些称为前向引用,可防止因 .h 文件相互引用而导致编译器错误。
I don't think this is best practice (also check syntax) however I have got away with:
in the .h
and in the .m
then I just assign the reference from somewhere convenient e.g. the app delegate or wherever you are instantiating your viewcontrollers.
then the view controller can get a reference to the other view controller.
I add @class secondviewcontroller to the .h file for the firstviewcontroller and put put the #imports "secondviewcontroller.h" in the .m file of the first view controller. These are called forward references and prevent compiler errors resulting from having .h files referencing each other.