Objective-C 中的委托和通知

发布于 2024-11-28 19:30:46 字数 285 浏览 1 评论 0原文

在我的 AppDelegate 中,我从 JSON feed 下载一些数据。我正在下载一些频道的现在/下一个/稍后的时间表。我现在、下一个和以后分别有三个不同的视图控制器。在每个视图控制器中,用户可以添加/删除频道,因此当发生这种情况时,必须再次下载新添加的频道数据。

由于数据是在 AppDelegate 中下载并存储在那里的,我如何将其传递给三个视图控制器?我应该实现三个独立的代表吗?请记住,添加新通道时,必须再次下载其数据(我现在正在 AppDelegate 之外执行此操作)。

有什么帮助吗?

谢谢

In my AppDelegate, I download some data from a JSON feed. I am downloading a now/next/later schedule for a few channels. I have three different view controllers for each now, next and later. In each view controller, a user can add/remove the channels so when that happens, the newly added channel data has to be downloaded again.

Since the data is downloaded in the AppDelegate and stored there, how would I pass it to the three view controllers? Should I implement three separate delegates? Keep in mind that when adding a new channel, its data has to be downloaded again (which I am doing outside the AppDelegate now).

Any help please?

Thanks

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

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

发布评论

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

评论(2

东北女汉子 2024-12-05 19:30:46

这基本上是一个风格问题,您的方法本身并不是无效的。

不过,我会用另一种方式来做——AppDelegate 并不意味着用作主力,同时拥有多个 AppDelegate 根本不可能。

以下是关于如何做到这一点的一些想法(尽管这当然不是唯一正确的方法):

  • 首先,我将使用核心数据或其他类型的存储,这些存储可以从应用程序中的任何位置使用。
  • 然后也许我会引入某种“数据控制器”类(可能是单例)。此类应根据请求处理数据下载以及将数据分发到视图控制器。
  • 通过为此目的配备一个中央控制器,您可以确保数据仅下载一次,因为控制器确切地知道哪些数据已在库存中。
  • 您的视图控制器将不再负责管理下载,也不会自行访问磁盘上的数据。他们只需向您的数据控制器发出请求,并在请求的数据可用时收到回调 - 无论数据是否已在磁盘上或已下载。
  • 这可以让您的风险投资人保持精简和专注,并减少更改界面的痛苦。

This is basically a matter of style and your approach isn't invalid as such.

I'd do it another way, though - the AppDelegate is not meant to be used as a workhorse and having several AppDelegates at the same time is simply impossible.

Here are some thoughts about how this could be done (though it's of course not the only proper way):

  • First, I'd employ Core Data or some other sort of storage which is available from anywhere within your app.
  • Then maybe I'd introduce some sort of "data controller" class (maybe a singleton). This class should handle both download of data and the distribution of that data to your viewcontrollers as requested.
  • By having one central controller for that purpose, you'd ensure that data gets downloaded only once because the controller knows exactly which data is already in stock.
  • Your viewcontrollers would neither be responsible for managing downloads anymore nor would they access the data on disk by themselves. They'd just make a request to your data controller and get a callback when the requested data is available - no matter if it was on disk already or has been downloaded for the occasion.
  • This keeps your VCs slim and focused and reduces the pain of making changes to your interface.
一袭白衣梦中忆 2024-12-05 19:30:46

Toastor 的答案是正确的,正如他所说,有多种方法可以做到这一点。一种是调用数据访问类并更改值或侦听值的更改。对于后者,键值观察编程指南 表示以下内容:

KVO 对于模型和模型之间的通信特别有用
应用程序中的控制器层。

  • 控制器对象通常观察模型的属性
    对象,视图对象观察模型对象的属性
    通过控制器。
  • 此外,模型对象还可以观察其他模型
    对象(通常用于确定依赖值何时发生变化)甚至
    本身(再次确定依赖值何时发生变化)。

另一种方法是使依赖关系明确,可能传递保存在应用程序委托中的数据访问类。请参阅面向对象的设计问题,iPhone

Toastor's answer is correct and as he says, there are several ways to do this. One is to call a data access class and change the values OR listen for changes on the values. For the later, the Key-Value Observing Programming Guide says the following:

KVO is particularly useful for communication between model and
controller layers in an application.

  • A controller object typically observes properties of model
    objects, and a view object observes properties of model objects
    through a controller.
  • In addition, however, a model object may observe other model
    objects (usually to determine when a dependent value changes) or even
    itself (again to determine when a dependent value changes).

Another is to make the dependency explicit, maybe passing a data access class saved in your app delegate. See Object-oriented design question, iPhone.

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