以编程方式导航到navigationcontroller内的viewcontroller&另一个故事板上的tabbarcontroller

时间:2017-04-05 15:14:57

标签: ios iphone swift uiviewcontroller uitabbarcontroller

我需要以编程方式从另一个Storyboard转到viewcontroller。从这里轻松。但问题是:viewController是嵌入在导航控制器中的,而这一个嵌入了tabBarController(见图片)

enter image description here

我试过了:

    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)

我得到的是:我转到正确的控制器,导航控制器工作,但没有tabbarcontroller

另一次尝试:

    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)

在这种情况下,tabbar正在运行,但没有导航控制器。

然后......尝试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)

在这种情况下,行为是如此奇怪......我出现在正确的控制器中,但有一个后退按钮。如果我点击我去同一个正确的控制器...使用tabbarcontroller ...然后navigationcontroller消失...很好:S

任何方式以编程方式运行到正确的控制器并使tabbar&导航?

1 个答案:

答案 0 :(得分:3)

尝试以下代码:

var storyBoard = UIStoryboard(name: "SecondStoryboard", bundle: nil)
var tabbar: UITabBarController? = (storyBoard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)
navigationController?.pushViewController(tabbar, animated: true)

我对swift的了解较少。上面的Objective-C代码是:

    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"SecondStoryboard" bundle:nil];
    UITabBarController *tabbar = (UITabBarController *)[storyBoard instantiateViewControllerWithIdentifier:@"tabbar"];
    [self.navigationController pushViewController:tabbar animated:YES];
相关问题