我有以下代码,在视图加载中添加观察者。
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserverForName:@"com.app.livedata.jsonupdated"
object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
NSLog(@"JSONUPDATED");
}];
}
这火力很好。但是,当视图被卸载并且我确认调用了 dealloc 时,通知仍在触发。
似乎没有办法停用这个观察者?
似乎解决方案是跟踪视图中的对象,然后您可以在 dealloc 方法中引用它。
id observer = [[NSNotificationCenter defaultCenter] addObserverForName: /* ... */ ];
然后删除如下:
[[NSNotificationCenter defaultCenter] removeObserver:observer];
observer = nil;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)