iOS:通知其他选项卡式视图控制器有关其数据集的更改
我有一个选项卡栏控制器,里面有两个控制器:一个地图视图控制器和一个表视图+ NSFetcheddata 控制器。两者都显示核心数据中特定日期的信息,并有一个按钮以模态方式显示日期选择器。
我已经实现了当模态视图控制器通过委托消失时控制器数据集发生变化,但我希望两个控制器更新其数据,而不仅仅是显示模态控制器的数据。
我考虑过在两个控制器中创建一个协议并将彼此设置为其代表,但我想知道我是否在这里做。
干杯,
蒂埃里
I have a tab bar controller and inside it two controllers: a mapview controller and a tableview + NSFetcheddata controller. Both display info about a specific day from core data and have a button to display a day selector modally.
I have achieved having my controllers dataset changing when their modal view controller disappears through delegation but I would like the two controllers to update their data and not only the one who displayed the modal controller.
I thought about creating a protocol in both controllers and setting each other as its delegate but I would like to know if I'm doing right here.
Cheers,
Thierry
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有很多不同的方法可以做到这一点。一种方法是使用 NSNotificationCenter。定义您自己的自定义通知名称:
在两个控制器中订阅此通知:
并实现
dataUpdated:
来更新您的数据:在导致更改的控制器中,发布通知:
There are tons of different ways to do this. One way is to use NSNotificationCenter. Define your own custom notification name:
Subscribe to this notification in both of your controllers:
And implement
dataUpdated:
to update your data:In the controller causing the change, post the notification:
您可以将两者设置为委托(即两个委托)并为两者重新使用模态视图控制器。
或者,使用NSNotificationCenter,
但我认为委托方法更好,因为这样关系更紧密。如果您想向多个对象发送消息,这是一种可行的方法。You could set both as the delegate (i.e. two delegates) and re-use your modal view controller for both.
Alternatively, use NSNotificationCenter,
but I think the delegate method is better, because the relationship is closer this way. which is the way to go if you want to message more than one object.蒂埃里,
作为 iOS 新手,我不会将此称为“答案”,但使用全局通知系统对我来说听起来并不适合此类问题。
在寻找类似问题的答案时,我偶然发现了对 NSFetchedResultsController 的引用,它将为您计算结果,很容易用作 UITableView 模型 - 仅阅读知识。与您的问题相关的部分似乎是它的委托 NSFetchedResultsControllerDelegate,它定义了一些方法,这些方法允许将结果的更改传达给任意数量的感兴趣的各方。
但正如我所说,我只是偶然发现了它,并且现在正在尝试利用它。
问候,诺比
Thierry,
being new to iOS, I wouldn't call this an "answer", but using a global notification system does not sound right for this kind of problem to me.
Looking for an answer to a similar problem, I stumbled over references to NSFetchedResultsController, which will calculate results for you, readily to be used as a UITableView model - only reading knowledge. The part relevant to your problem seems to be its delegate, NSFetchedResultsControllerDelegate, which defines a few methods which will allow to communicate changes to the result to any number of interested parties.
But as I said, I just stumbled over it, and are just now trying to make use of it.
Regards, nobi