removeObserver with NSNotification… what am I doing wrong?

前端 未结 3 1383
终归单人心
终归单人心 2020-12-28 14:59

Basically, I have a view1 which at some point, calls view2 (via presentModalViewController:animated:). When a certain UIButton in view2 is pressed,

3条回答
  •  孤独总比滥情好
    2020-12-28 16:02

    Calling -dealloc doesn't automatically happen after the view controller is dismissed — there can still be some "life" left in the view controller's lifetime. In that timeframe, that view controller is still subscribed for that notification.

    If you remove the observer in -viewWillDisappear: or -viewDidDisappear:, this will have a more immediate effect:

    - (void) viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                        name:@"alert" 
                                                      object:nil];
    }
    

提交回复
热议问题