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?

4

2 回答 2

2

不是 dealloc - 请参阅此问题进行讨论。

最好在 ViewWillDisappear 中进行。我通常在观察开始时设置一个布尔“isObserving”标志,然后在调用 ViewWillDisappear 时进行检查。

eta-最佳实践表明它实际上应该在两者中完成,但 dealloc 只是一个支持。

eta#2:对于 ARC,dealloc 仅在引用计数降至零时调用。如果它正在观察,则引用仍然存在,因此不会调用 dealloc。

于 2013-09-06T09:00:53.180 回答
0

我通常调用我的子视图/子视图控制器removeObserver- (void)dealloc方法。弹出后DetailModal它应该被释放。

于 2013-09-06T08:57:00.460 回答