我有三种方法:
- (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!");
}
主要思想是,对于这个类,我需要接收 2 个不同的通知,并且在收到它们的情况下需要执行不同的操作。实现一切都好吗?我相信可以简化此代码。如何removeObserver
正确?我不使用ARC。