繁体   English   中英

无法在popViewControllerAnimated上解包可选

[英]Can't unwrap Optional on popViewControllerAnimated

我知道错误Can't unwrap optional意味着程序正试图解开一个nil变量。 麻烦是我无法弄清楚在哪里。

我正在编写一个以模态方式呈现的编辑屏幕。 用户更改了内容,将其保存,然后返回到显示更新对象的“详细信息”屏幕。

我在第二个VC上,我到处都是断点,并且在线上触发了崩溃

self.navigationController.popViewControllerAnimated(true)

我在VC中有断点,我正在返回并且没有运行代码,因此我无法理解程序尝试解包的可选项。

有任何想法吗?

编辑:它是否与VC有关我正在返回被解除分配?

而且,你最初的问题是self.navigationControllernil ,所以你试图访问它时崩溃。 如果你以模态方式呈现,你也可以这样解雇:

self.presentingViewController.dismissViewController(true)

为了使它始终有效,您可以:

if self.navigationController {
    self.navigationController.popViewControllerAnimated(true)
}
else if self.presentingViewController {
    self.presentingViewController.dismissViewController(true)
}
else {
    // Unknown presentation
}
    let segueLabel = self.storyboard!.instantiateViewControllerWithIdentifier("LabelViewController") as! LabelViewController
    let segueGeluid = self.storyboard!.instantiateViewControllerWithIdentifier("SoundViewController") as! SoundViewController

    if (indexPath.row == 0) {
        self.navigationController?.pushViewController(segueLabel, animated: true)
    }
    else if (indexPath.row == 2) {
        self.navigationController?.pushViewController(segueGeluid, animated: true)
    }

在navigationController上使用pushViewController时,navigationController会推送新的ViewController。 导航将存在。 我认为这是最好的解决方案。 使用以下代码,您可以轻松返回到之前的ViewController:

    self.navigationController?.popViewControllerAnimated(true)

弄清楚了。 我用错了方法来解除控制器。 它是模态呈现的,所以它没有响应popViewControllerAnimated - 我需要实现一个协议和委托模式,以便主VC可以解雇它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM