准备功能导致崩溃

时间:2018-10-24 14:20:16

标签: ios swift

在我的快捷应用程序中,我具有以下准备功能:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let destination = segue.destination as? LocalPictureCVC {
        destination.selectedLocal = selectedLocal
    }
}

但是当我尝试在LocalPictureCVC中使用selectedLocal时,由于selectedLocal为nil而崩溃。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

尝试在您的prepare函数中检查您的segue标识符

if segue.identifier == "Something"

答案 1 :(得分:0)

尝试一下,希望对您有所帮助:-

首先请确保正确提供带有标识符的标记,然后再执行以下代码 然后在第一个ViewController中

var valueToPassInSecondVC = ""

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    //NOTE:- make sure about your identifier is correct as well as destination ViewController
    if segue.identifier == "yourIdentifierOfSegue" {
        let destVC = segue.destination as! ContainerViewController
        destVC.valueFromFirstVC = self.valueToPassInSecondVC
    }
}

您的目的地班级

var valueFromFirstVC:String = ""
//In my case pass String from FirstViewController to SecondViewController

谢谢,希望对您有帮助。