[英]Navigate programmatically to viewcontroller inside navigationcontroller & tabbarcontroller on another storyboard
I need to go programmatically to a viewcontroller from another Storyboard. 我需要以编程方式从另一个Storyboard转到viewcontroller。 From here easy.
从这里轻松。 But the problem is: that viewController is embedded in a navigation controller, and this one into a tabBarController (see image)
但问题是:viewController嵌入在导航控制器中,而这一个嵌入了tabBarController(见图)
I've tried: 我试过了:
let storyboard = UIStoryboard(name: "Inside", bundle: nil)
let viewcontroller = storyboard.instantiateViewController(withIdentifier: "controller_id")
let navigationController = UINavigationController(rootViewController: viewcontroller)
self.present(navigationController, animated: true, completion: nil)
What I get with this is: I go to the correct controller, with navigation controller working, but no tabbarcontroller 我得到的是:我去了正确的控制器,导航控制器工作,但没有tabbarcontroller
Another try: 另一个尝试:
let storyboard = UIStoryboard(name: "Inside", bundle: nil)
let tabbar = storyboard.instantiateViewController(withIdentifier: "tab_id")
let navigationController = UINavigationController(rootViewController: tabbar)
self.present(navigationController, animated: true, completion: nil)
In this case, tabbar is working, but no navigationcontroller. 在这种情况下,tabbar正在工作,但没有navigationcontroller。
And... try number 3: 并且...尝试3号:
let storyboard = UIStoryboard(name: "Inside", bundle: nil)
let tabbar = storyboard.instantiateViewController(withIdentifier: "tab_id")
let viewcontroller = storyboard.instantiateViewController(withIdentifier: "race_id")
let navigationController = UINavigationController(rootViewController: tabbar)
navigationController.pushViewController(viewcontroller, animated: false)
self.present(navigationController, animated: true, completion: nil)
In this case, the behaviour is so strange... I appear in the correct controller, but with a back button. 在这种情况下,行为是如此奇怪...我出现在正确的控制器,但有一个后退按钮。 If I click I go to the same correct controller... with the tabbarcontroller... and then navigationcontroller disappear... nice :S
如果我点击我去同一个正确的控制器...使用tabbarcontroller ...然后navigationcontroller消失...很好:S
Any way to go programmatically to the correct controller and get working the tabbar & navigation? 有没有办法以编程方式进入正确的控制器并开始使用tabbar和导航?
Try out below code: 试试下面的代码:
var storyBoard = UIStoryboard(name: "SecondStoryboard", bundle: nil)
var tabbar: UITabBarController? = (storyBoard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)
navigationController?.pushViewController(tabbar, animated: true)
I have less knowledge in swift. 我对swift的了解较少。 Objective-C code for the above is:
上面的Objective-C代码是:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"SecondStoryboard" bundle:nil];
UITabBarController *tabbar = (UITabBarController *)[storyBoard instantiateViewControllerWithIdentifier:@"tabbar"];
[self.navigationController pushViewController:tabbar animated:YES];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.