iPhone:如何在选项卡栏应用程序中的多个视图控制器之间传递数据

发布于 2024-08-24 01:48:30 字数 404 浏览 3 评论 0原文

我有以下问题:

我构建了一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬色的秋千 2024-08-31 01:48:30

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

数据模型是可从应用程序中的任何位置访问的定制的独立对象。数据模型对象对任何视图或视图控制器一无所知。它只存储数据以及数据之间的逻辑关系。

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

在正确设计的应用程序中,任何两个视图控制器都不应有权访问另一个控制器的内部数据。 (视图控制器需要知道另一个控制器存在的唯一原因是它是否必须触发另一个控制器的加载。)

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

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

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

编辑:

为了澄清您的具体情况,您可以在接收器 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:

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.

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 a UITabBar 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.

简单气质女生网名 2024-08-31 01:48:30

您可能需要考虑 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.

Saygoodbye 2024-08-31 01:48:30

我不认为这是最佳实践(还要检查语法),但是我已经摆脱了:

在 .h

otherclassref *otherclassname

@property (assign) otherclassname otherclassref;

和 .m 中

@synthesize otherclassref;

,然后我只是从方便的地方分配引用,例如应用程序委托或实例化视图控制器的任何地方。

然后视图控制器可以获得对另一个视图控制器的引用。

我将 @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

otherclassref *otherclassname

@property (assign) otherclassname otherclassref;

and in the .m

@synthesize otherclassref;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文