我正在尝试调用根控制器,但我不知道调用它的代码。我使用的是条件控件,它将检查以前的视图控制器是什么,并且取决于它的内容,将运行一段代码。这是我的代码:

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
     //}
}

最佳答案

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)

关于ios - 在Swift中调用上一个 View Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30177183/

10-11 15:02