-2

I have a screen with 2 buttons, A and B. When you press the buttons they go to the same view, with a button C. When pressed C depending if you pressed A or B they go to different views.

I have something like this, but I have two problems. First, it't not working, even when I hardcode Button A to mealTableViewController.from. Second, I don't know how to pass witch button was pressed to mealTableViewController.from.

class HomeViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard let mealTableViewController = segue.destination as? MealTableViewController else {
            return
        }
        mealTableViewController.from = "Button A"
    }
}

In the storyboard attached to HomeViewController there are two button with a Show segue to Meal Table View Controller

Then, I have MealTableViewController, with a button connected to the action boton, that depending the value of from, it transition to one screen or to other.

class MealTableViewController: UITableViewController {
    var from: String?

    @IBAction func boton(_ sender: UIButton) {
        if from == "Button A" {
            performSegue(withIdentifier: "ShowMealView", sender: self)
        } else {
          performSegue(withIdentifier: "ShowOther", sender: self)
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()
    }
}
alo gon
  • 167
  • 1
  • 3
  • 16
  • 1
    Segues have identifiers. In the *Attributes Inspector* assign `"buttonA"` to the segue from button A, and `"buttonB"` from buttonB. Check the identifier in `prepare(for:sender:)` with `if segue.identifier == "buttonA" { mealTableViewController.from = "Button A" } else if segue.identifier == "buttonB" { mealTableViewController.from = "Button B" }` – vacawama Aug 21 '19 at 16:43
  • Can you explain how you connected the HomeViewController to the MealTableViewController in the storyboard? As in, did you ctrl+drag from the button A/B to the MealTableViewController? – TheAppMentor Aug 21 '19 at 17:24
  • 2
    Possible duplicate of [Passing Data between View Controllers in Swift](https://stackoverflow.com/questions/24222640/passing-data-between-view-controllers-in-swift) and a large number of other questions – dandan78 Aug 21 '19 at 20:09

1 Answers1

0

I tried to reproduce this example and it works as expected.

Button A action perform segue with identifier Button A

Button B action perform segue with identifier Button B

MealTableViewController has segues attached with identifiers ShowMealView and ShowOther(not Button C)

Storyboard:

enter image description here

FirstViewController

class FirstViewController: UIViewController {

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

        guard let mealTableViewController = segue.destination as? MealTableViewController else {
            return
        }

        mealTableViewController.from = segue.identifier

    }

}

MealTableViewController

class MealTableViewController: UIViewController {

    var from: String?

    @IBAction func boton(_ sender: UIButton) {
        if from == "Button A" {
            performSegue(withIdentifier: "ShowMealView", sender: self)
        } else {
            performSegue(withIdentifier: "ShowOther", sender: self)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

}

Check your segue identifiers and where they are attached to:

enter image description here

enter image description here

alxlives
  • 5,084
  • 4
  • 28
  • 50