In a master-detail application, I have the following layout;
Master Let's assume we have a UITableView here
Detail Shows more information about a item from the Master. We setup a observer in NSNotificationCenter here
DetailModal Shows something, is shown from Detail
Where is the correct way of calling removeObserver
if I still want to receive notifications when Detail or DetailModal is shown but remove it when we go back to Master?
The way I see it, I would set a flag to not remove the observer when showing the DetailModal and check for that flag in viewWillDissapear
. Is this a good approach?
Not dealloc - see this question for a discussion.
It's best to do it in ViewWillDisappear. I usually have a boolean "isObserving" flag that is set when observing starts and then checked when ViewWillDisappear is called.
eta- best practice suggests it should actually be done in both, but the dealloc is only a backstop.
eta#2: With ARC, dealloc is only called when the reference count falls to zero. If it's observing, a reference still exists and so dealloc never gets called.