是否有方法检索标签栏控制器当前的可见导航控制器?
例如,我的程序中有2个标签栏(每个导航控制器一个),如下所示
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
//Method is called when user clicks on a hyperlink in one of view controllers
NSDictionary *dict = [self parseQueryString:[url query]];
NSString *userID = [dict objectForKey:@"id"];
NSString *navconTitle = [dict objectForKey:@"navcon"];
//intention is to push a view controller onto the CURRENT navigation stack
[navcon pushViewController:someViewController animated:YES];
}
}
return YES;
}
任何人都可以告诉我如何确定当前的导航控制器,以便我可以将更多的视图控制器推到它上面吗?
答案 0 :(得分:58)
使用UITabBarController
s selectedViewController属性。
navcon = (UINavigationController*)myTabBarController.selectedViewController;
[navcon pushViewController:someViewController animated:YES];
答案 1 :(得分:3)
我认为UITabBarController selectedViewController
属性应该是你想要的。
所以,从UITabBarController方法: -
[self.selectedViewController pushViewController:someViewController animated:YES];
答案 2 :(得分:1)
一个Swift版本,以防有人无法阅读Objective-C,并提供了一个附加的解决方案,用于从任何地方查找tabBar。处理通知时,我用它来推送到屏幕。
guard let tabBarVC = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController else { return }
if let currentNavController = tabBarVC.selectedViewController as? UINavigationController {
currentNavController.pushViewController(someVC, animated: true)
}
答案 3 :(得分:0)
将Starsky的Swift答案更新到iOS 13(iOS 13.0中已弃用“ keyWindow”)
guard let tabBarVC = UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.rootViewController as? UITabBarController else { return }
if let currentNavController = tabBarVC.selectedViewController as? UINavigationController {
...
}