簡體   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