新增使用塊刪除觀察者
可以使用塊來代替新增帶選擇器的觀察者:
id testObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"TestNotification"
object:nil
queue:nil
usingBlock:^(NSNotification* notification) {
NSDictionary *userInfo = notification.userInfo;
MyObject *myObject = [userInfo objectForKey:@"someKey"];
}];
然後可以刪除觀察者:
[[NSNotificationCenter defaultCenter] removeObserver:testObserver
name:@"TestNotification"
object:nil];