Objective-C 中的委托和通知
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这基本上是一个风格问题,您的方法本身并不是无效的。
不过,我会用另一种方式来做——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):
Toastor 的答案是正确的,正如他所说,有多种方法可以做到这一点。一种是调用数据访问类并更改值或侦听值的更改。对于后者,键值观察编程指南 表示以下内容:
另一种方法是使依赖关系明确,可能传递保存在应用程序委托中的数据访问类。请参阅面向对象的设计问题,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:
Another is to make the dependency explicit, maybe passing a data access class saved in your app delegate. See Object-oriented design question, iPhone.