当我触摸选项卡栏项目时,我没有收到任何通知
我有 UITabbarCoo=ntroller 应用程序。我添加了观察者,正在等待任何通知。当我触摸选项卡栏项目时,我没有收到任何通知。
[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"])
{
}
}
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"])
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我注意到同样的事情。我认为这是 UITabBarController 实现中的一个错误。请注意,使用
selectedViewController
的键路径而不是selectedIndex
确实会导致 KVO 通知被触发。但要小心。如果您的 UITabBarController 有一个 UIMoreNavigationController (用于“更多”选项卡),当用户选择“更多”选项卡时您将收到 KVO 通知,但您不会当用户选择 UIMoreNavigationController 的子视图控制器时收到任何通知。这是因为 UIMoreNavigationController 是一个单独的视图控制器,因此当您选择其子视图控制器之一时,UITabBarController 的
selectedViewController
不会更改 – 它实际上是 UIMoreNavigationController 的topViewController
> 这会改变。如果除了 UITabBarController 的
selectedViewController
属性之外,您还可以观察 UIMoreNavigationController 的topViewController
属性,那就太好了,但此属性似乎也不会导致 KVO 通知被触发。但是,您可以在 UIMoreNavigationController 上设置委托并实现navigationController:didShowViewController:animated:
方法。摘要:观察 UITabBarController 的
selectedViewController
属性,如果您的应用程序有“更多”选项卡,请在选项卡栏控制器的moreNavigationController
上设置委托> 财产。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 ofselectedIndex
does cause KVO notifications to be fired.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'stopViewController
that changes.It would be great if you could then observe the UIMoreNavigationController's
topViewController
property in addition to the UITabBarController'sselectedViewController
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 thenavigationController:didShowViewController:animated:
method.Summary: observe the
selectedViewController
property of the UITabBarController, and if your application has a "More" tab, set a delegate on the tab bar controller'smoreNavigationController
property.