NSNotification...the right way?

856 Views Asked by At

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?

1

There are 1 best solutions below

3
On BEST ANSWER

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.