Adding and removing observers to NSNotificationCenter in a UIViewController

拟墨画扇 提交于 2019-12-02 20:40:18
Lorenzo B

There are a lot of discussions about removing notifications in the right way. For example:

I suggest you to remove observers in viewWillDisappear (or viewDidDisappear) and viewDidUnload lifecycle methods. (Note: viewDidUnload was deprecated and shouldn't be implemented in iOS6+; see iOS 6 - viewDidUnload migrate to didReceiveMemoryWarning?)

An important note:

viewDidUnload is not guaranteed to be called - it's not a standard lifecycle method.

From Apple doc:

viewDidUnload When a low-memory condition occurs and the current view controller’s views are not needed, the system may opt to remove those views from memory. This method is called after the view controller’s view has been released and is your chance to perform any final cleanup.

Instead, dealloc is called whenever the number of references for that receiver is zero.

Hope it helps.

Edit

For the sake of completeness you can see this link on how to avoid-nsnotification-removeobserver. The link provide some useful guidelines to remove observer (see also the comments). The author does it in viewDidAppear/viewDidDisappear methods since viewWillAppear and viewWillDisappear are not always called correctly in many applications. It's your choice.

If you want to be sure to remove observers in the right way unregister it in dealloc method or when the view is fully unloaded as you wrote in the second comment. But be sure that dealloc will be call in the future. In other words, as I already mentioned, if the controller continues to stay alive since some other object has a referenced to it, the method will never get called. In this case the controller continues to receive notifications.

- (void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];
   [[NSNotificationCenter defaultCenter] addObserver:self .........]
}

- (void)viewWillDisappear:(BOOL)animated
{
   [super viewWillDisappear:animated];
   [[NSNotificationCenter defaultCenter] removeObserver:self .........];
}

For folks stumbling on this page more recently, the removal of observers might not be necessary anymore. The "Discussion" section of the addObserver(_:selector:name:object:) docs say:

If your app targets iOS 9.0 and later or macOS 10.11 and later, you don't need to unregister an observer in its dealloc method. Otherwise, you should call removeObserver(_:name:object:) before observer or any object passed to this method is deallocated.

Why would you not do it in viewWillAppear / viewDidDisappear? You only care about the notifications when your view is showing anyway, right?

Sola Zhou

You can addObserver in viewWillAppear,and removeObserver in viewWillDisappear. but viewWillAppear may called many times. so you can remove Notification first then addObserver.

 -(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIKeyboardWillShowNotification" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIKeyboardWillHideNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)name:@"UIKeyboardWillShowNotification"object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)name:@"UIKeyboardWillHideNotification"object:nil];
 }

 -(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIKeyboardWillShowNotification" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIKeyboardWillHideNotification" object:nil];
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!