Objective C:在一个类中设置 2 个 NSNotifications 有什么问题吗?

发布于 2024-11-16 19:40:02 字数 707 浏览 6 评论 0原文

我有一个实现了 2 个 NSNotifications 的类,

    //Set up notifications
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(getData)
                                                 name:@"Answer Submitted"
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(reloadTable)
                                                 name:@"Comment Submitted"
                                               object:nil];

我只想检查是否可以在一个类中设置 2 个观察者?另外,当我删除观察者时,我仅在 dealloc 方法中删除一个观察者。这是一个问题吗?

I have a class with 2 NSNotifications implemented

    //Set up notifications
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(getData)
                                                 name:@"Answer Submitted"
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(reloadTable)
                                                 name:@"Comment Submitted"
                                               object:nil];

I would just like to check if it is ok to set 2 observers in a single class? Also when I remove observer, I am only removing one observer in dealloc method. Is that an issue?

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

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

发布评论

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

评论(1

英雄似剑 2024-11-23 19:40:02

在一个班级中有多个观察员是完全可以的。一旦你完成了观察者的注册,你应该始终注销它。

有关 Objective-C 中观察者模式的更多详细信息,请参见 这里

It is perfectly fine to have more than one observer in a single class. You should always unregister the observer once you're done with it.

More details on the Observer pattern in Objective-C can be found here.

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