当我触摸tabbar项目时,我没有收到任何通知

我有UITabbarCoo = ntroller应用程序。 我添加了一个观察者,我正在等待任何通知。 当我触摸tabbar项目时,我没有收到任何通知。

[self.tabBarController addObserver:self forKeyPath:@"selectedIndex" options:NSKeyValueObservingOptionNew context:@"changedTabbarIndex"]; - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSString *action = (NSString*)context; if([action isEqualToString:@"changedTabbarIndex"]) { } } 

我注意到了同样的事情。 我假设这是UITabBarController实现中的一个错误。 请注意,使用selectedViewController的关键path而不是selectedIndex会导致KVO通知被触发。

不过要小心。 如果您的UITabBarController具有UIMoreNavigationController (对于“更多”选项卡),则当用户select“更多”选项卡时,您将获得KVO通知,但当用户selectUIMoreNavigationController的子视图控制器时,您将不会收到任何通知。 这是因为UIMoreNavigationController是一个单独的视图控制器,所以当你select一个子视图控制器时,UITabBarController的selectedViewController不会改变 – 它实际上是UIMoreNavigationController的topViewController改变。

如果你可以观察UIToreBarController的topViewController属性,还可以观察UIMoreNavigationController的topViewController属性,但是这个属性看起来不会导致KVO通知被触发。 但是,您可以在UIMoreNavigationController上设置委托并实现navigationController:didShowViewController:animated:方法。

简介:观察UITabBarController的selectedViewController属性,如果您的应用程序具有“更多”选项卡,请在选项卡栏控制器的moreNavigationController属性上设置一个委托。