NSNotification...正确的方法?
我有一个选项卡栏应用程序,两个选项卡使用相同的类(不同的实例)来显示 UITableView。根据单击的选项卡,对外观、功能等进行了一些细微的更改。我遇到的问题是,从 UITableView 中我显示了一个模式视图,当它被关闭时,它会发布一个 NSNotification 来重新加载 UITableView (由UITableView 的视图控制器),但我收到 2 个 NSNotifications,因为我的应用程序中有 2 个同一类的实例。如何让通知仅发布在调用它的实例中?
I have an tab bar app and the 2 tabs use the same class (different instances) to show a UITableView. Based on which tab is clicked a few minor changes are made to the appearance, functionality etc. The problem I have is that from the UITableView I show a modal view and when it is dismissed it posts an NSNotification to reload the UITableView (handled by the UITableView's view controller), but I get 2 NSNotifications posted as there are 2 instances of the same class in my app. How can I get the notification be be posted in just the instance it is called from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您为
NSNotification
设置处理程序时,您可以指定一个您感兴趣的通知的对象
。您应该将第一个表视图控制器设置为仅对通知感兴趣从模态视图控制器的特定实例发布,而您的第二个表视图控制器只对从模态视图控制器的特定实例发布的通知感兴趣:
这样,当发布通知时从模态视图控制器中,只有指定其兴趣的表视图控制器才会处理通知。
When you set up a handler for an
NSNotification
you can specify anobject
for whose notifications you are interested in.You should set your first table view controller to only be interested in notifications that are posted from the specific instance of the modal view controller, and your second table view controller to only be interested in notifications posted from its specific instance of the modal view controller:
This way when a notification is posted from your modal view controller, only the table view controller that has specified its interest will handle the notification.