Reputation: 2923
I have three methods:
- (void)viewDidAppear:(BOOL)animated
{
[self updateViews];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"itemQuantityChanged" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:[NSString stringWithFormat:@"Item %@ deleted", itemUUID] object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void) receiveNotification: (NSNotification *)notification
{
if ([[notification name] isEqualToString:@"itemQuantityChanged"])
[self updateViews];
if ([[notification name] isEqualToString:[NSString stringWithFormat:@"Item %@ deleted", itemUUID]])
NSLog(@"FAIL!");
}
The main idea is that for this class I need to receive 2 different notifications and in case of receiving them need to perform different actions. Is everything ok with the realization? I believe that it is possible to simplify this code. How to removeObserver
correctly? I don't use ARC.
Upvotes: 0
Views: 74
Reputation: 119242
You should use a different selector for each notification. That way, you don't need any logic in the method to determine which notification was sent.
Removing the observers as you are doing is fine.
Upvotes: 2