ios 如何从标签栏控制器获取“当前”导航控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6441153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:29:18  来源:igfitidea点击:

How to get the 'current' navigation controller from tab bar controller

objective-ciosuinavigationcontrolleruitabbarcontroller

提问by Zhen

Is there is a method to retrieve tab bar controller's current visible navigation controller?

有没有一种方法可以检索标签栏控制器的当前可见导航控制器?

For example, I have 2 tabbars in my program (one navigation controller each) as below

例如,我的程序中有 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;
}

Can anyone advise me how I can determine the current navigation controller so that I can push more viewcontrollers onto it?

谁能告诉我如何确定当前的导航控制器,以便我可以将更多的视图控制器推到它上面?

回答by Joe

Use the UITabBarControllersselectedViewController property.

使用UITabBarController小号selectedViewController财产。

navcon = (UINavigationController*)myTabBarController.selectedViewController;
[navcon pushViewController:someViewController animated:YES];

回答by Adrian B

I think UITabBarController selectedViewControllerproperty should be what you are looking for.

我认为UITabBarController selectedViewController财产应该是你正在寻找的。

So, from a UITabBarController method :-

因此,从 UITabBarController 方法:-

 [self.selectedViewController pushViewController:someViewController animated:YES];

回答by Starsky

A Swift version, in case someone can't read Objective-C, with an additional solution for how to find the tabBar from anywhere. I use this to push to a screen when processing a notification.

一个 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)
}