UIViewController在XCode 7中不再具有成员topViewController或viewControllers

Fil*_*Fil 4 ios xcode7 swift2

更新到XCode 7并将我的项目转换为最新的Swift 2语法后,有一个我似乎无法解决的错误.我有一个导航控制器的segue,需要将数据传递到其堆栈中的顶视图控制器.以下一直有效:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let destinationVC = segue.destinationViewController.viewControllers[0] as! MYViewController
    // OR let destinationVC = segue.destinationViewController.topViewController as! MYViewController
    // ...
}
Run Code Online (Sandbox Code Playgroud)

但是现在编译器给出了错误:

Value of type 'UIViewController' has no member 'viewControllers' 要么 Value of type 'UIViewController' has no member 'topViewController'

我没有看到如何访问堆栈上的视图控制器.有任何想法吗?提前致谢!

Zig*_*ong 9

as! UINavigationController之后添加segue.destinationViewController以便转换为UINavigationController类类型.

let destinationVC = (segue.destinationViewController as! UINavigationController).viewControllers[0] as! MYViewController
Run Code Online (Sandbox Code Playgroud)

要么

let destinationVC = (segue.destinationViewController as! UINavigationController).topViewController as! MYViewController
Run Code Online (Sandbox Code Playgroud)