我有两个视图,当您从一个切换到另一个时,它们会向即将加载的视图调用通知以刷新内容。奇怪的是,第一次加载视图时,它会调用一次,下一次调用两次,以此类推。我得出的结论是,这是因为每次加载视图时都会不断添加它们。由于 dealloc 永远不会被调用,它仍然存在并且现在会继续添加。
那么有没有办法在被添加来解决这个问题之前检查通知是否存在?
这是我的 viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReloadGridNotification:) name:@"ReloadOHGridView" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReloadBadgeNotification:) name:@"reloadBadge" object:nil];
还有我的交易:
[[NSNotificationCenter defaultCenter] removeObserver:self];
谢谢!
库尔顿
编辑1:
我在 UINavigationController 中显示我的视图并在它们之间切换。这是我刷新不同视图的代码:
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadBadge" object:self];
}