0

I want to assign button tag to variable. Then I trying send this data to SecondViewController and assign it to var.

@IBAction func goToProducts(_ sender: UIButton) {
    
    performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: self)
    categoryNumber = sender.tag }

Pass data to ProductsViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "goToProducts" {
        let destinationController = segue.destination as! ProductsViewController
        destinationController.categoryNumb = categoryNumber
    }
}

At ProductsViewController I create var but there isn't this data. I print this variable and I get this:

categoryNumber: 373

categoryNumb: Optional(24)

Can you tell me haw can I do this?

1
  • for starters, if you are using a String Constant as your segue id (which isn't a bad idea, although using an enum is probably more common) then you should use it in prepare(for:) too. If you've got a slight difference between the literal & the constant the value won't be injected. Commented Nov 14, 2019 at 19:47

1 Answer 1

1

You need to assign it first before performSegue

categoryNumber = sender.tag  
performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: self)

OR use sender

performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: sender.tag)  

Pass data to ProductsViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "goToProducts" {
        let destinationController = segue.destination as! ProductsViewController
        destinationController.categoryNumb = sender as! Int
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.