Wednesday, 21 August 2013

How to receive 2 notifications via NSNotificationCenter

How to receive 2 notifications via NSNotificationCenter

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.

No comments:

Post a Comment