iPhone:如何在Tabbar应用程序中的多个Viewcontrollers之间传递数据 [英] iPhone: How to Pass Data Between Several Viewcontrollers in a Tabbar App

查看:98
本文介绍了iPhone:如何在Tabbar应用程序中的多个Viewcontrollers之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题:

我已经构建了一个带有4个标签的tabbar应用程序。我想将对象/变量从第一个选项卡控制器传递到第三个控制器控制器,并使用相应的对象初始化此控制器。

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.

我已经做过一些研究。对应于干净模型方法的最佳方法是在被调用的viewcontroller上调用一些initWithObject:方法。
我怎样才能做到这一点?如何在调用者控制器中调用receivercontroller的 init 方法?你能给我一些代码示例吗?

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?

编辑:
要在几个视图/类之间传递数据等,只需创建一些数据类来保存数据。在几个类之间共享。欲了解更多信息,请访问以下链接:
Singleton

推荐答案

您需要一个存储应用程序数据的数据模型对象。

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.

当应用程序的不同部分需要写入或读取数据时,它们会对数据模型进行写入和读取。在您的情况下,view1会在卸载时将其数据保存到数据模型中,然后view2会在加载时从数据模型中读取该数据(反之亦然。)

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.)

快速而肮脏的方式创建数据模型是向app delegate添加属性,然后使用以下命令从视图控制器调用app delegate:

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:

YourAppDelegateClass *appDelegate = [[UIApplication sharedApplication] delegate];
myLocalProperty = appDelegate.someDataModelProperty;

这适用于小项目,但随着您的数据变得复杂,您应该为您创建一个专用的类数据模型。

This will work for small project but as your data grows complex, you should create a dedicated class for your data model.

编辑:

为了澄清您的具体情况,您可以在调用时添加对数据模型的调用。 receiver viewController变为活动状态。

To clarify for your specific case, you would add the call to the data model when the receiver viewController becomes active.

将数据放在init方法或 viewDidLoad 中将无效,因为在 UITabBar 用户可以在不卸载视图或重新初始化视图控制器的情况下来回切换。

Placing the data in an init method or a viewDidLoad won't work because in a UITabBar the users can switch back and forth without unloading the view or reinitializing the view controller.

检索更改数据的最佳位置是 viewWillAppear 控制器方法。这样,每次用户切换到该选项卡时,数据都会更新。

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.

这篇关于iPhone:如何在Tabbar应用程序中的多个Viewcontrollers之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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