iPhone开发中对象之间传递数据的最佳方式?

发布于 2024-09-29 02:49:47 字数 418 浏览 7 评论 0原文

我正在开发 iPhone 应用程序,但不知道跨视图保存临时数据的最佳方法是什么。例如,我在服务器上有 xml,我将数据解析为一个对象,以便我可以保存信息。 xml 中有一些我想在一个视图上显示的数据,以及我想在另一个视图上显示的其他数据。我有一个名为 dataStore 的类,我想在视图中保存数据,iPhone 的最佳做法是什么?

我研究了很多选项,我认为这是一个选择:

1)使用数据存储作为委托并将数据保存在可以访问的委托中。

2)将dataStore用作单例类,并且只允许该类的一个实例并从dataStore类的共享实例访问数据。

3)甚至可能是一个我不知道但熟悉通过应用程序上下文和实例传递数据的android方式的NSMangedObjectContext。

如果有人可以帮助我选择 iPhone 上的最佳实践,我将不胜感激。

I am working on an iphone application and don't know what the best way to hold temporary data across views. for example i have xml on a server and i parse the data into an object so that I can hold the information. The xml has some data that i want to be displayed on one view and other data that i want to be displayed on another view. I have a class called dataStore that I want to hold the data across the views, what is the best iphone practice to do that?

I have looked into many options and I think its a choice between:

1) use the dataStore as a delegate and hold the data in a delegate thaat can be accessed.

2) use the dataStore as a singleton class and only allow one instance of the class and access the data from the shared instance of the dataStore class.

3) maybe even an NSMangedObjectContext which i dont know about but am familiar with the android way of passing data through an application context and instance.

If someone could help me with choosing which is the best practice on the iphone i would be much greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

请叫√我孤独 2024-10-06 02:49:47

您可以使用 NSNotification 宣布数据已到达。您可以作为发送者或在信息字典中将临时对象与通知一起发送。然后,需要部分数据的两个对象可以将这些部分拉出并立即保留它们,临时数据对象可以是这样:临时的,仅持续到通知完成为止。只有您的视图需要和关心的部分才会保留,并且每个视图控制器都可以在有意义的时候释放其部分。

You could announce that the data has arrived using an NSNotification. You can send the temporary object along with the notification, as either the sender or in the info dictionary. The two objects that need parts of that data can then pull those parts out and retain those right then, and the temporary data object can be just that: temporary, lasting only till the notification is complete. Only the parts your views need and care about hang around, and each view controller can release its part whenever it makes sense.

痴情换悲伤 2024-10-06 02:49:47

对于您的情况,我建议将 dataStore 类转换为单例。在单例中,解析数据并具有返回视图所需信息的方法。如果您希望数据在启动过程中保持不变,那么核心数据(托管对象)是很好的选择。虽然很常见,但将数据存储在 appDelegate 中是我选择的最后一个选项。

In your case I'd recommend turning the dataStore class into a singleton. In the singleton, parse the data and have methods that return the required information for the view. Core Data (managedObjects) are good if you want the data to persist across launches. While common, storing data in the appDelegate is last option I'd pick.

身边 2024-10-06 02:49:47
  1. 不,你不能让 dataStore 成为委托。您永远不应该保留您的委托,因此它不是存放数据的好地方,而只能存放行为。

  2. 很可能是单身人士。您这里所拥有的可能是您的应用程序的模型,您的应用程序是否应该能够处理两个模型?可以同时显示多个文档吗?不要编写一行代码来尝试阻止单例的客户端创建第二个实例! Objective-C 是动态运行时,持久用户无论如何都可以这样做,因此您的代码所做的就是添加更多具有潜在错误的代码行。

  3. 除非确实需要,否则不要为 Core Data 和 NSManagedObjectContext 操心。只有当您打算处理模型中的数据超出 RAM 所能容纳的数据时,您才需要这样做。

  1. No you can not let the dataStore be a delegate. You should never retain your delegates, so it is not a good place for data, only for behavior.

  2. Most probably a singleton. What you have here is probably a model fro your app, is there any reason why your app should be able to handle two models? Can it show many documents simultaneously? Do not write a single line of code in order to try to stop a client of your singleton from creating a second instance! Objective-C is a dynamic run-time and a persistent user will be able to anyway, so all your code do is add more lines of code with potential bugs.

  3. Do not bother with Core Data and a NSManagedObjectContext until you really need to. And you only need to if you intent to handle more data in your model than can ever fit into RAM.

天冷不及心凉 2024-10-06 02:49:47

这些都不是真正的 MVC。您需要三个类:

  • 模型,其中包含数据,
  • 视图,通常完全在 IB 中作为 .xib 完成,
  • 视图 >视图控制器,它是.xib的文件所有者,并将数据作为实例字段。这个负责将模型的数据传递给视图,视图将视图控制器设置为其委托数据源。这样,视图就可以重用,并且只处理一小部分数据(即与实际可见的数据完全相同)。

None of those are truly MVC. You want three classes:

  • The model, which contains the data,
  • The view, often done entirely in IB as a .xib,
  • The view controller, which is the .xib's File's Owner and has the data as an instance field. This one is responsible for passing the model's data to the view, and the view sets the view controller as its delegate and dataSource. This way, the view can be reusable, and only ever handles a fraction of the data (i.e. exactly as much as is actually visible).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文