问题描述
我有UITabbarCoo = ntroller应用程序.我添加了一个观察者,正在等待任何通知.触摸标签栏项时没有收到任何通知.
I have UITabbarCoo=ntroller application. I added an observer and I'm waiting for any notifications. I didn't get any notifications when I touched on tabbar items.
[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
代替selectedIndex
确实会触发KVO通知.
I noticed the same thing. I assume that this is a bug in the UITabBarController implementation. Note that using a key path of selectedViewController
instead of selectedIndex
does cause KVO notifications to be fired.
但是要小心.如果您的UITabBarController具有 UIMoreNavigationController (用于更多"标签),则当用户选择更多"标签时您会收到KVO通知,但您不会当用户选择UIMoreNavigationController的子视图控制器时,将获得任何通知.这是因为UIMoreNavigationController是一个单独的视图控制器,因此当您选择其子视图控制器之一时,UITabBarController的selectedViewController
不会更改-实际上是UIMoreNavigationController的topViewController
会更改.
But be careful. If your UITabBarController has a UIMoreNavigationController (for the "More" tab), you will get the KVO notification when the user selects the "More" tab, but you won't get any notifications when the user selects a child viewcontroller of the UIMoreNavigationController. This is because the UIMoreNavigationController is a separate view controller, so when you select one of its child view controllers, the UITabBarController's selectedViewController
isn't changing – it's actually the UIMoreNavigationController's topViewController
that changes.
如果您随后可以观察到UIMoreNavigationController的topViewController
属性以及UITabBarController的selectedViewController
属性,那就太好了,但是此属性似乎也不会导致KVO通知被触发.但是,您可以 在UIMoreNavigationController上设置一个委托并实现navigationController:didShowViewController:animated:
方法.
It would be great if you could then observe the UIMoreNavigationController's topViewController
property in addition to the UITabBarController's selectedViewController
property, but this property does not appear to cause KVO notifications to be fired either. However, you can set a delegate on the UIMoreNavigationController and implement the navigationController:didShowViewController:animated:
method.
摘要::观察UITabBarController的selectedViewController
属性,如果您的应用程序具有更多"标签,请在标签栏控制器的moreNavigationController
属性上设置一个委托.
Summary: observe the selectedViewController
property of the UITabBarController, and if your application has a "More" tab, set a delegate on the tab bar controller's moreNavigationController
property.
这篇关于触摸标签栏项时没有收到任何通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!