How to stop the Observer in NSNotification to called twice?

前端 未结 7 1190
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 20:08

I have an observer of NSNotification which is called twice. I do not know what to do with it.

I googled it but no solution found.

[[NSNo         


        
7条回答
  •  无人及你
    2020-12-02 20:18

    If your addObserver method is run multiple times, it will create multiple observers. My issue was that I somehow placed my observer in viewWillAppear which appeared multiple times before I posted the notification and it resulted in my observer being called multiple times.

    While EmptyStack's 3rd solution works, there is a reason your observer is being called twice, so by finding it, you can prevent needless lines of code instead of removing and adding the same observer.

    I would suggest putting your observer in viewDidLoad to avoid simple errors like the one I experienced.

提交回复
热议问题