如何使用swift从视图控制器调用SetRootViewController?

时间:2014-10-12 19:19:14

标签: ios objective-c swift ios8

我试图在UIViewController中使用swift中的setRootViewController,而不是来自app delegate。

我发现了一些关于堆栈溢出的问题,但是他们已经解释过从app delegate那里做的所有问题。但是,我想从UIViewController中创建它并将UITabBarController设置为rootViewController

这就是我在目标c中做到的方式

UITabBarController *main = [self.storyboard instantiateViewControllerWithIdentifier:@"main"];

[[[[UIApplication sharedApplication] delegate] window] setRootViewController:main];

我想在swift中做同样的事情。有什么方法可以在swift中完成,还是我需要使用segue?

1 个答案:

答案 0 :(得分:2)

我使用以下代码行完成:

UIApplication.sharedApplication().delegate?.window??.rootViewController = mainViewController