removeObserver with NSNotification… what am I doing wrong?

前端 未结 3 1361
终归单人心
终归单人心 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 15:44

    If you implement the removal of Observer in the viewWillDisappear: or viewDidDisappear: then you should not leave the addition of the observer in the viewDidLoad.

    Instead put the addition of the observer in the viewWillAppear:. The problem you are having is because when any view is shown onto of the UIViewController view the removal of your observer will occur and since you added observer in viewDidLoad which will happen only once, it will be lost.

    Keep in mind that this approach works well for objects you do not wish to observer while your main view is not in the fore front.

    Also Keep in mind that viewDidUnload has been depreciated too.

提交回复
热议问题