如何让 unwind segue 在同一视图控制器中与常规 segue 一起工作?

How to get unwind segue to work between along side a regular segue in the same view controller?

抱歉,如果标题有点含糊。我仍在学习 swift 和 segues,我有 3 个视图控制器,分别称为 EarthViewControllerMoonViewControllerJupiterViewController

这些是有效的 segues:

EarthViewController -> MoonViewController
MoonViewController -> JupiterViewController
JupiterViewController -> MoonViewController, 
and even JupiterViewController -> EarthViewController, 

但是 MoonViewController -> EarthViewController 的 unwind segue 不断抛出 Thread 1: Signal SIGABRT 错误,我在尝试返回地球视图时评论了 //ERROR HERE。这是到目前为止的布局和代码:

地球ViewController (ViewController)

import UIKit

class ViewController: UIViewController {

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

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let des = segue.destination as! MoonViewController 

        if (segue.identifier == "toMoonView")
        {
            print("Went to the moon")
        }
    }

    @IBAction func backFromMoon(segue: UIStoryboardSegue){

        if let scVC = segue.source as? MoonViewController
        {
            print ("Back from the Moon to Earth")
        }
    }

    @IBAction func backFromJupiterToEarth(segue: UIStoryboardSegue){

        if let scVC = segue.source as? JupiterViewController
        {
            print ("Back from Jupiter to Earth")
        }
    }
}

月亮ViewController

import UIKit

class MoonViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

//------------------------------------------------------------------------
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let des = segue.destination as! JupiterViewController //<- ERROR HERE

        if (segue.identifier == "toJupiterView")
        {
            print("Went to Jupiter")
        }
    }
//-----------------------------------------------------------------------
    @IBAction func backFromJupiter(segue: UIStoryboardSegue){

        if let scVC = segue.source as? JupiterViewController
        {
            print ("Back from Jupiter to the Moon")
        }
    }
}

JupiterViewController(尚未添加任何内容)

import UIKit

class JupiterViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }


}

非常感谢任何帮助!谢谢!

MoonViewController中的这个函数:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let des = segue.destination as! JupiterViewController //<- ERROR HERE

    if (segue.identifier == "toJupiterView")
    {
        print("Went to Jupiter")
    }
}

为常规转场和展开转场调用。当您转到 JupiterViewController 时,segue.destination 确实是 JupiterViewController 类型。但是当你展开到 ViewController 时,segue.destination 的类型是 ViewController.

在第二种情况下,强制转换 as! JupiterViewController 崩溃是因为类型错误。

解决此问题的一种方法是在确定目的地后将 des 的声明移动到 if 中:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toJupiterView"
    {
        let des = segue.destination as! JupiterViewController
        print("Went to Jupiter")
    }
}