尝试与导航视图控制器绑定时出错 无法在强制中将 'UIViewController' 类型的值转换为 'UINavigationController' 型



实际错误Cannot convert value of type 'UIViewController' to type 'UINavigationController' in coercion

在我进入我的导航之前,在视图控制器的代码中。

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "SegueAfterJoin2") {
       // NOTE! THIS IS TWEAKED with "as UINavigationController"
        var nextViewCont:DashboardNavViewController = segue.destinationViewController as UINavigationController;
        // PASS VARS FROM THIS VIEW TO NEXT
        nextViewCont.passedPassword_toViewDashboard = passedPassword_toView2
        nextViewCont.passedEmail_toViewDashboard = passedUsername_toView2
        nextViewCont.passedUsername_toViewDashboard = passedEmail_toView2
        nextViewCont.passedFocus_toViewDashboard = selectedFocus
    }
}

我的导航视图控制器类定义为:class DashboardNavViewController: UIViewController { .它应该被宣布为别的东西吗?

只需替换

var nextViewCont: DashboardNavViewController = segue.destinationViewController as UINavigationController

var nextViewCont = segue.destinationViewController as! DashboardNavViewController

如果你想呈现UINavigationController那么你的DashboardNavViewController必须是UINavigationController的子类,现在是UIViewController的子类,所以你不能像你的错误所说的那样UINavigationController投射它。

您的导航视图控制器类应该像下面这样被污损:

class DashboardNavViewController: UINavigationController {