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

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

问题描述

我有以下问题:

我构建了一个带有 4 个选项卡的选项卡栏应用程序.我想将一个对象/变量从第一个选项卡控制器传递到第三个,并用相应的对象初始化这个控制器.

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.

我已经做了一些研究.与干净模型方法相对应的最佳方法是在被调用的视图控制器上调用一些 initWithObject: 方法.我怎样才能做到这一点?如何在 callercontroller 中调用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?

要在多个视图/类等之间传递数据,只需创建某种数据类,该类保存多个类之间共享的数据.欲了解更多信息,请点击链接:单身

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

推荐答案

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

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

创建数据模型的快速而肮脏的方法是向应用程序委托添加属性,然后使用以下方法从视图控制器调用应用程序委托:

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.

为了阐明您的具体情况,您将在接收者 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 应用程序中的多个视图控制器之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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