相同 class 中的两个 "performSegue" 到不同的 Viewcontroller

Two "performSegue" in same class to different Viewcontrollers

我正在尝试使用两个不同的按钮转到两个不同的 VC。使用 'newGeneratedListBtn,我只想转到 vc1。使用 'newEmptyList' 按钮,我想使用有效的 bool 值转到 vc3。但是,当我单击第一个按钮 'newGeneratedListBtn' 转到 vc1 时,出现错误:“无法将类型 'AppName.vc1' 的值转换为 'AppName.vc3'。

好像当我点击按钮到 vc1 时,它试图发送与我想发送到 vc3 相同的值。我该如何解决?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


let value = true

//Button to VC1
@IBAction func newGeneratedListBtn(_ sender: UIButton) {
    performSegue(withIdentifier: "unwindSegueToVC1", sender: self)
}

//Value to send to VC2
override func prepare (for segue: UIStoryboardSegue, sender: Any!) {

        let vc = segue.destination as! ListView
        vc.newEmptyListValue = value
}

//Button to VC2
@IBAction func newEmptyList(_ sender: UIButton) {

    performSegue(withIdentifier: "valueSender2", sender: self)
}

}

您需要为特定标识符进行转换

override func prepare (for segue: UIStoryboardSegue, sender: Any!) {
    super.prepare(for:segue,sender:sender)
    if segue.identifier == "valueSender2" {
       let vc = segue.destination as! ListView
       vc.newEmptyListValue = value 
    }
    else {
       // it's unwindSegueToVC1
    }
}

同时

performSegue(withIdentifier: "unwindSegueToVC1", sender: self) 
performSegue(withIdentifier: "valueSender2", sender: self)

随叫随到prepare

问题是prepare(for:sender:)只有一个实现,像这样:

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

        let vc = segue.destination as! ListView
        vc.newEmptyListValue = value
}

你的两个 segues 都通过了那个方法。您必须使用开关或其他条件来确定这是哪个转场,并发送适合该转场的信息。