2

I have added NSNotificationCenter in viewDidLoad method and removed in viewDidUnload but it's not getting removed. I am following ARC. I have followed few answer but I didn't get luck. I dont have reputation for give comments so posting some thing looks like duplicate. Please don't -ve votes.

Sample code:

- (void)viewDidLoad
{     
    [[NSNotificationCenter defaultCenter ] addObserver:self.containerView
                                              selector:@selector(loadInitialScreen)
                                                  name:CLEARSCREEN_DEPOSIT  
                                                object:NULL];
}

- (void)viewDidUnload
{
    [[NSNotificationCenter defaultCenter] removeObserver:self.containerView
                                                    name:CLEARSCREEN_DEPOSIT 
                                                  object:nil];

}
4

3 回答 3

4

根据您的需要,您应该在 或 方法-viewWillDisappear:中删除观察者-viewDidDisappear:-dealloc原因是-viewDidUnload在 iOS6+ 中不再被调用,而在 iOS6 之前,它在收到内存警告时被调用。

于 2013-02-27T07:29:11.780 回答
1

Try to use viewDidDisappear instead viewDidUnload :

-(void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self.containerView
                                                    name:CLEARSCREEN_DEPOSIT object:nil];
    [super viewDidDisappear:animated];
}
于 2013-02-27T07:23:09.610 回答
0

当应用程序/视图控制器收到内存警告时,会调用 viewDidUnload(对于 < iOS 6.0)。删除视图时不会调用它,因为调用了 dealloc。但是当你使用 ARC 时,你不能实现 dealloc 方法。

最好的办法是删除方法中的观察者loadInitialScreen,如果它只需要调用一次的话。

如果你的通知可以多次发布,最好在 viewDidDisappear 中移除观察者,然后在 ViewWillAppear 中为通知添加观察者

于 2013-02-27T07:34:17.287 回答