加载中...

正确移除addObserverForNameUsingBlock的姿势


@property (nonatomic, strong) id observer;

- (void) viewDidLoad {
  self.observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification
                                                      object:nil 
                                                      queue:[NSOperationQueue mainQueue]
                                                  usingBlock:^(NSNotification *note) {
                                                      NSLog("hello");
                                                  }];
}

- (void)dealloc {
  [[NSNotificationCenter defaultCenter] removeObserver:observer];
	observer = nil;
   NSLog(@"__dealloc__");
}

文章作者: km
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 km !