-1

My app has a category of things so when I press "Burger" button on my home viewcontroller it takes me to secondviewcontroller with more options (Buttons) like "spicy, mild, Fire". what I want to do is I want to save path of those button clicked on thirdviewcontroller as text.

For example on thirdviewcontroller a text says "Burger -> Spicy" or "Burger -> mind" make sure I'm dealing with the button pressed in the path and not text entered.

How do I do that

Asif Mujtaba
  • 447
  • 6
  • 17

1 Answers1

0

One easy way is to keep two variables in the second and third view controller.

class FirstViewController : UIViewController {
    
    @IBAction func buttonAction(_ sender: UIButton) {
        let vc2 = ...// instantiate second view controller
        vc2.textVal = sender.titleLabel!.text
        //present vc2
    }
}

Then in the second view controller get the text of the button and set it in a text

class SecondViewController : UIViewController {
    
    var textVal: String?
    
    @IBAction func buttonAction(_ sender: UIButton) {
        let vc3 = ...// instantiate thrid view controller
        let buttonText = sender.titleLabel!.text
        vc3.textVal = "\(textVal) -> \(buttonText)"
        //present vc3
    }
}

Now in the third view controller set the label's text

class ThridViewController : UIViewController {
    
    var textVal: String?
    @IBOutlet weak var lblText: UILabel!
    
    override func viewDidLoad() {
        lblText.text = textVal
    }
}

Hope it will help you.

Asif Mujtaba
  • 447
  • 6
  • 17
  • it's not working! multiple errors. i think the method is really right but code wise i think there is problem – Information Technology Jul 14 '20 at 03:08
  • I didn't write full code here, just the prototype, you need to instantiate 2nd and 3rd view controller, then you have to present/push view controller. Do that for yourself. I just want to show you how to pass buttons data from one view controller to another. – Asif Mujtaba Jul 14 '20 at 03:53
  • sorry im so a beginner to this that i do not even understand that much – Information Technology Jul 14 '20 at 04:11
  • then look at [this](https://stackoverflow.com/questions/24035984/instantiate-and-present-a-viewcontroller-in-swift) and [this](https://stackoverflow.com/questions/42470334/how-to-instantiate-a-new-view-controller-programmatically) answer for how to instantiate & present/push view controller. – Asif Mujtaba Jul 14 '20 at 05:36