简体   繁体   中英

Should I unregister NSNotification both in viewDidUnload and dealloc?

I register a NSNotification in viewDidLoad method.

Should I unregister it both in viewDidUnload and dealloc method using the code below?

[[NSNotificationCenter defaultCenter] removeObserver:self];

Thanks.

Yes you should. viewDidUnload is not called when the view controller is deallocated.

Because viewDidLoad is called when the view controller is opened, people sometimes mistakenly assume that its opposite (viewDidUnload) is called when the screen closes. That is not the case, viewDidUnload is only used in low-memory situations.

That's why we need to unregister for the notifications in dealloc as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM