【问题标题】:Custom Segue in Swift 2.0 : destinationViewController's navigationController property returning nilSwift 2.0 中的自定义 Segue:destinationViewController 的 navigationController 属性返回 nil
【发布时间】:2016-06-14 20:51:50
【问题描述】:

我有一个从 ViewController A 到 ViewController B 的 segue。它使用自定义 segue 类 RightSegue。这是 RightSegue 类的代码:

class RightSegue: UIStoryboardSegue {

override func perform() {

    // Assign source and destination view controllers to local variables.
    let vc1: UIViewController = self.sourceViewController
    let vc2: UIViewController = self.destinationViewController

    // Get screen width and height.
    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height

    // get the source view controller's frame.
    let vc1Frame = vc1.view.frame

    let vc2NavController = vc2.navigationController
    let vc2NavFrameHeight = vc2NavController!.navigationBar.frame.size.height

    // Specify the initial position of the destination view. (x, y, width, height)
    vc2.view.frame = CGRectMake(screenWidth, vc2NavFrameHeight, screenWidth, screenHeight)

    // Push the destinationViewController onto sourceViewController.
    vc1.navigationController!.pushViewController(vc2, animated: false)

    // Specify the initial position of the source view. Add destination view as a subview of the source view.
    vc1.view.frame = CGRectMake(vc1.view.frame.origin.x, vc1.view.frame.origin.y, screenWidth, vc1Frame.size.height)
    vc2.navigationController?.view!.addSubview(vc1.view!)

    // Animate!
    UIView.animateWithDuration(0.25, animations: {() -> Void in

        vc1.view.frame = CGRectMake(-screenWidth, vc1.view.frame.origin.y, vc1.view.frame.size.width, vc1.view.frame.size.height)
        vc2.view.frame = CGRectMake(0.0, 0.0, vc2.view.frame.size.width, vc2.view.frame.size.height)

        }, completion: {(finished: Bool) -> Void in

            vc1.view!.removeFromSuperview()

    })

}

视图控制器的设置如图所示。

可以看出,FirstViewController 包含一个按钮,单击该按钮会显示 SecondViewController。

下一张图片显示了 segue 的属性:

当我执行应用程序并按下 FirstViewController 中的按钮时,应用程序崩溃并显示错误消息:

fatal error: unexpectedly found nil while unwrapping an Optional value

我在 RightSegue 类中检查了vc2NavController。它的返回类型为UINavigationController?。强制解包返回 nil。

如此答案 (Custom Segue in Swift) 中所示,我添加了 @objc() 并运行了应用程序。它仍然没有工作。应用程序崩溃,这次给出错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not create a segue of class '(null)''

我不知道该怎么办。请帮忙。

【问题讨论】:

    标签: ios uiviewcontroller swift2 segue uistoryboardsegue


    【解决方案1】:

    编辑: 您需要推送您的第二个 viewController 以使其拥有一个 navigationController。 自定义 segue 是导致您的问题的原因。您需要返回 navigationController,或者更好 - 请阅读:

    http://netsplit.com/custom-ios-segues-transitions-and-animations-the-right-way

    如果您想保持习惯,这可能会有所帮助 - Custom Push Segue removes navigation bar and tab bar in story board

    【讨论】:

    • 刚才试了一下,报错Could not cast value of type 'Custom_Segue.SecondViewController' (0x1035872a0) to 'UINavigationController' (0x104c87588).
    • 我的错,我以为你有一个导航控制器作为第二个视图。
    • 抱歉这么久才回复。你的回答很完美。根据您链接的文章,我使用了自定义动画器,而不是自定义 segues。谢谢!我将很快发布代码以及一个工作示例。
    • 完美。以这种方式切换到使用自定义转换(我遇到的所有其他示例都不适用于导航控制器)效果很好......而且也很干净!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多