Marco
Marco

Reputation: 1061

Swift one out of three segue is not passing data

I am trying to pass data to another controller with a segue after clicking on a button.

This is my code:

@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
            }
        }
    }

My problem is that the first two segues are working perfectly fine. The third one (PuppyClassSegue) is not passing the data through. It opens the page but the data are not showing, basically the destination.course is not happening.

Any idea how is that possible?

Thank you!

enter image description here

Upvotes: 1

Views: 53

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

It appears that course is nil , also you can do this by hooking all buttons to the same action and setting tags for them 0,1,2 respectively

@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
            }
        }
}

make 1 segue named say SegueFromCon from the current vc to the detailsVC

Upvotes: 1

Related Questions