以编程方式在swift中导航viewcontroller

时间:2015-12-17 05:37:09

标签: swift

我想提供一个名为BaseViewController的视图控制器,它应该有导航,以便我可以从BaseViewController导航。我尝试使用以下代码,但它没有帮助

@IBAction func presentBaseViewControllerButtonAction(sender: AnyObject) {

    let baseViewController = getViewControllerInstance("Base", storyboardId: "BaseViewControllerID") as! PostServiceViewController

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds) //error this viewcontroller has no member window
    var navigationController = UINavigationController()
    var mainView = baseViewController(nibName: nil, bundle: nil) 
    navigationController.viewControllers = [mainView]
    self.window!.rootViewController = navigationController
    self.window?.makeKeyAndVisible()

    self.presentViewController(baseViewController, animated: true, completion: nil)
}

我找到了答案。如果还有人为此而苦苦挣扎,那就是

let navigationController = UINavigationController(rootViewController: setupController) navigationController.navigationBarHidden = true self.presentViewController(navigationController, animated: true, completion: nil)

3 个答案:

答案 0 :(得分:0)

试试这个

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var nav = UINavigationController()
var mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
nav.viewControllers = [mainView]
self.window!.rootViewController = nav
self.window?.makeKeyAndVisible()

答案 1 :(得分:0)

试试这个

let baseViewController = getViewControllerInstance("Base", storyboardId: "BaseViewControllerID") as! PostServiceViewController
let nav = UINavigationController(rootViewController: baseViewController!)
self.window?.rootViewController = nav

答案 2 :(得分:0)

您需要将storyboard id作为ViewController名称

    let next = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardController") as! DashboardController
    self.presentViewController(next, animated: true, completion: nil)