Can I watch an NSNotification from another class?
Asked Answered
I

5

6

I'm trying to get my head around NSNotificationCenter. If I have something like this in my App Delegate:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(something:) 
                                                 name:@"something"
                                               object:nil];
-----

-(void)something:(NSNotification *) notification
{
  // do something

}

Can I somehow watch this in another view controller? In my case, I'd like to watch it in a view controller with a table, and then reload the table when a notification is received. Is this possible?

Interlocutory answered 18/8, 2011 at 20:11 Comment(0)
K
4

Yes you can that is the whole purpose of NSNotification, you just have to add the View Controller you want as an observer exactly the same way you did on your App Delegate, and it will receive the notification.

You can find more information here: Notification Programming

Karalynn answered 18/8, 2011 at 20:14 Comment(1)
@Cannyboy Yes you can add as many observers to a single notification as you want.Karalynn
S
13

Yes you can do it like this:

In class A : post the notification

 [[NSNotificationCenter defaultCenter] postNotficationName:@"DataUpdated "object:self];

In class B : register first for the notification, and write a method to handle it. You give the corresponding selector to the method.

//view did load
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleUpdatedData:) name:@"DataUpdated" object:nil];


-(void)handleUpdatedData:(NSNotification *)notification {
    NSLog(@"recieved");
}
Storz answered 18/8, 2011 at 20:19 Comment(1)
and then where do I remove the observer?Nagy
K
4

Yes you can that is the whole purpose of NSNotification, you just have to add the View Controller you want as an observer exactly the same way you did on your App Delegate, and it will receive the notification.

You can find more information here: Notification Programming

Karalynn answered 18/8, 2011 at 20:14 Comment(1)
@Cannyboy Yes you can add as many observers to a single notification as you want.Karalynn
S
2

Of course it's possible, that's the whole point of notifications. Using addObserver:selector:name:object: is how you register to receive notifications (you should do this in your table view controller), and you can use postNotificationName:object:userInfo: to post a notification from any class.

Read Notofication Programming Topics for more info.

Splurge answered 18/8, 2011 at 20:15 Comment(0)
A
2

You can register to observe notifications in as many classes as you like. You simply need to "rinse and repeat". Include the code to register as an observer in your view controller (perhaps in viewWillAppear:) and then reload the tableView from your method:

- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(something:) name:@"something" object:nil];
}

-(void)something:(NSNotification *) notification
{
  [self.tableView reloadData];
}

It's also a good idea to de-register the view controller once you no longer need the notifications:

- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
Allege answered 18/8, 2011 at 20:20 Comment(0)
A
1

You should just add that as an Observer and give a different selector method if you want that viewController to behave differently when that notification is posted.

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(somethingOtherThing:) 
                                             name:@"something"
                                               object:nil];


-(void)somethingOtherThing:(NSNotification *) notification
{
// do something

}
Alialia answered 18/8, 2011 at 20:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.