What's the better way to addObserver/removeObserver with NSNotificationCenter?

前端 未结 5 1413
孤城傲影
孤城傲影 2020-12-08 15:07

I used to addObserver in viewDidLoad: and removeObserver in dealloc:. Code:

- (void)viewDidLoad
{
    [su         


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-08 15:35

    I would normally put it in -viewDidAppear: and -viewDidDisapear: (or -viewWillAppear: and -viewWillDisappear:) simply because in every case I came across I'm only interested in the notification if the view is actually displayed.

    It's probably a premature optimisation (your code for handling the notification could take some time, but might be useless if the view is not displayed), but then it's also no more code - it's the same code just in a different method...

提交回复
热议问题