iOS:通知其他选项卡式视图控制器有关其数据集的更改

发布于 2024-12-01 09:51:38 字数 246 浏览 2 评论 0原文

我有一个选项卡栏控制器,里面有两个控制器:一个地图视图控制器和一个表视图+ 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 技术交流群。

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

发布评论

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

评论(3

绅士风度i 2024-12-08 09:51:38

有很多不同的方法可以做到这一点。一种方法是使用 NSNotificationCenter。定义您自己的自定义通知名称:

static NSString *const CSDataUpdatedNotification = @"CSDataUpdatedNotification";

在两个控制器中订阅此通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataUpdated:) name:CSDataUpdatedNotification object:nil];

并实现 dataUpdated: 来更新您的数据:

- (void)dataUpdated:(NSNotification *)notification
{
    // Handle updates here
}

在导致更改的控制器中,发布通知:

- (void)updateData
{
    // Data updating routine
    // ...

    [[NSNotificationCenter defaultCenter] postNotificationName:CSDataUpdatedNotification object:self];
}

There are tons of different ways to do this. One way is to use NSNotificationCenter. Define your own custom notification name:

static NSString *const CSDataUpdatedNotification = @"CSDataUpdatedNotification";

Subscribe to this notification in both of your controllers:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataUpdated:) name:CSDataUpdatedNotification object:nil];

And implement dataUpdated: to update your data:

- (void)dataUpdated:(NSNotification *)notification
{
    // Handle updates here
}

In the controller causing the change, post the notification:

- (void)updateData
{
    // Data updating routine
    // ...

    [[NSNotificationCenter defaultCenter] postNotificationName:CSDataUpdatedNotification object:self];
}
梦中的蝴蝶 2024-12-08 09:51:38

您可以将两者设置为委托(即两个委托)并为两者重新使用模态视图控制器。

或者,使用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.

爱给你人给你 2024-12-08 09:51:38

蒂埃里,
作为 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

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