Swift 三分之一的 segue 没有传递数据

Swift one out of three segue is not passing data

我试图在单击按钮后使用 segue 将数据传递给另一个控制器。

这是我的代码:

@IBAction func agilityDogBtnPressed(_ sender: Any) {
    if let objs = controller.fetchedObjects, objs.count > 0 {
        let course = objs[0]
            self.performSegue(withIdentifier: "DogAgilitySegue", sender: course)
    }
}
@IBAction func baseEducationBtnPressed(_ sender: Any) {
    if let objs = controller.fetchedObjects, objs.count > 0 {
        let course = objs[1]
        self.performSegue(withIdentifier: "BaseEducationSegue", sender: course)
    }
}
@IBAction func puppyBtnPressed(_ sender: Any) {
    if let objs = controller.fetchedObjects, objs.count > 0 {
        let course = objs[2]
        self.performSegue(withIdentifier: "PuppyClassSegue", sender: course)
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "DogAgilitySegue" {
        if let destination = segue.destination as? ClassDetailsVC {
            if let course = sender as? Course {
                destination.course = course
            }
        }
    } else if segue.identifier == "PuppyClassSegue" {
        if let destination = segue.destination as? ClassDetailsVC {
            if let course = sender as? Course {
                destination.course = course
            }
        }
    } else if segue.identifier == "BaseEducationSegue" {
        if let destination = segue.destination as? ClassDetailsVC {
            if let course = sender as? Course {
                destination.course = course
            }
        }
    }

我的问题是前两个 segue 工作得很好。 第三个 (PuppyClassSegue) 没有传递数据。 它打开了页面但没有显示数据,基本上 destination.course 没有发生。

知道这怎么可能吗?

谢谢!

看起来课程是 nil ,您也可以通过将所有按钮挂钩到相同的操作并分别为它们设置标签 0,1,2 来做到这一点

@IBAction func agilityDogBtnPressed(_ sender: UIButton) {
    if let objs = controller.fetchedObjects, objs.count > 0 {
        let course = objs[sender.tag]
            self.performSegue(withIdentifier: "SegueFromCon", sender: course)
} 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
        if let destination = segue.destination as? ClassDetailsVC {
            if let course = sender as? Course {
                print("hit here ",course)
                destination.course = course
            }
        }
}

制作 1 个名为 say SegueFromCon 的 segue 从当前 vc 到 detailsVC