1

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?

hank
  • 3,748
  • 1
  • 24
  • 37

2 Answers2

2

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.

Community
  • 1
  • 1
Will Jenkins
  • 9,507
  • 1
  • 27
  • 46
0

I usually call removeObserver on the - (void)dealloc method of my subview / subview controller. After popping the DetailModal it should be released.

Nimrod Gutman
  • 1,027
  • 1
  • 10
  • 21