我正在尝试调用根控制器,但我不知道调用它的代码。我使用条件,它将检查以前的视图控制器是什么,并且代码块将根据它是什么运行。这是我的代码:
SecondViewController
override func viewDidLoad ( ) {
super.viewDidLoad ( )
let previousController = storyboard!.instantiateViewControllerWithIdentifier ("firstViewController") as? FirstViewController
//HELP IN LINE BELOW
//if root view controller == previousController {
//run some code
//}
}
答案 0 :(得分:8)
let stack = self.navigationController.viewControllers
if (stack.count > 1) {
let previousController = stack[stack.count-2]
}
或者如果您没有导航控制器
let previousController = self.presentingViewController
另外,为了清楚起见,根视图控制器将是堆栈中的第一个视图控制器,即:
self.navigationController.viewControllers.firstObject
或
var previousController : UIViewController = self
var rootController : UIViewController?
do {
var rootViewController = previousController
var previousController = rootViewController.presentingViewController
} while (previousController != nil)