开发者

NSNotification...the right way?

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?


When you set up a handler for an NSNotification you can specify an object 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:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handler:)
                                             name:notificationName 
                                           object:instanceOfModalViewController];

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜