Objective C:在一个类中设置 2 个 NSNotifications 有什么问题吗?
我有一个实现了 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在一个班级中有多个观察员是完全可以的。一旦你完成了观察者的注册,你应该始终注销它。
有关 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.