我在UITabBarController
上有两个标签,我已在NSNotificationCenter
注册了两个标签,我的问题是我没有收到隐藏标签上的通知(即它还没有被称为viewDidAppear:
)。我的想法是,不在屏幕上(即隐藏)的控制器不响应NSNotifications
。我可以用不同的方式做事情,这不是问题,但我只是想验证为什么隐藏的标签没有得到通知,以防我错过了其他的东西,它应该实际上正在工作......
@Fab1n指出了我正确的方向,我错误地使用了viewWillDisappear:
来移除观察者,所以当视图消失时,不再听取通知。我会将其移至dealloc
。
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self];
}
更改为:
- (void)dealloc {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self];
}
非常感谢。
答案 0 :(得分:1)
据我所知,任何标签栏元素都是延迟加载的,尽管第一个。它们在您第一次点击代表标签栏项目时(或通过以编程方式访问其视图属性)加载。之后,即使您选择其他标签项,他们也可以收到您想要的任何信息。
答案 1 :(得分:1)
为了确保安全:在NSNotification
或(与IB)-init
-initWithNibName:
当您设置UITabbarController
的{{1}}属性时,init
的控制器会被controllers
初始化。
UITabbarController
和viewWillAppear
。
<强>解决方案:强>
viewDidAppear
来注册您的NSNotification
-init
中的通知(请勿使用ARC拨打-dealloc
)现在一切正常!