开发者

Where should I remove a notification observer?

I set up a notification observer in my view controller's init method like so:

[[NSNotificationCenter defaultCenter] 
                    addObserver:self
                    selector:@selector(saveState)
                    name:UIApplicationWillResignActiveNotification
                    object:nil];

Where is the best place to call removeObserver:name:object: for this notificatio开发者_运维技巧n. I'm currently calling it in my dealloc method, but wanted to know if that might cause problems.


No, you got it right. dealloc is the correct location to remove notification observers (unless you have some specific reason to need to remove the observer earlier).


You can always remove the observer in viewWillDisappear:, or when you are done using it and have no other need for it, you can place it in a function.


If the -saveState only need to execute once when active, then you can removeObserver inside the -saveState.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜