2016-04-03 119 views
0

我有一个UIViewController与一个按钮。如何在swift中从我的UIViewController设置rootViewController?

此按钮有一个方法,应该切换到另一个UIViewController。以前我曾用它来处理:

self.performSegueWithIdentifier("moveToMainWindow", sender: self) 

,但我想改变它的东西,如:

let sb = UIStoryboard(name: "Main", bundle: nil) 
if let tabBarVC = sb.instantiateViewControllerWithIdentifier("MainWindow") as? TabController { 
    self.rootViewController = tabBarVC 

} 

但是这条线self.rootViewController = tabBarVC带来错误类型的ViewController的值没有任何成员RootViewController的。

有什么我可以在这里修复,以便它可能工作?

我想避免做performSegueWithIdentifier,因为后来我在做几次self.dismissViewControllerAnimated(true, completion: {}),在这种情况下,我的MainWindow面板也被解雇了。所以我想如果我在这里改变segue的类型,这个面板永远不会被隐藏。

回答

1

rootViewController属性是UIWindow的属性,它定义了在应用程序启动时显示的第一个或根视图控制器,并且您无法将其设置为视图控制器上的属性。

你可以做什么,而不是推动视图控制器与赛段,是通过故事板启动视图控制器,然后推动它使用showViewController:presentViewController:方法在UIViewController。你已经几乎得到它:

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
if let tabBarVC = storyboard.instantiateViewControllerWithIdentifier("MainWindow") as? TabController { 
    self.showViewController(tabBarVC, sender: self) 
} 

另外请注意,您需要调用dismissViewController:completion:tabBarVC

相关问题