1

I'm learning how to pass data between controllers when a button is pressed, so I'm following this answer: Pass data through segue, But I can't make it work, I have 3 buttons and I want to know which button was pressed, so my code in HomeView is:

   var buttonPressed: String?

    @IBAction func newsTapped(_ sender: Any) {

        buttonPressed = "news"
        self.performSegue(withIdentifier: "showNext", sender: buttonPressed)
    }


    @IBAction func tipsTapped(_ sender: Any) {
        buttonPressed = "tips"
        self.performSegue(withIdentifier: "showNext", sender: buttonPressed)
    }


    @IBAction func otherTapped(_ sender: Any) {
        buttonPressed = "other"
        self.performSegue(withIdentifier: "showNext", sender: buttonPressed)
    }


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let data = buttonPressed
        if let destinationViewController = segue.destination as? TableViewController {
                destinationViewController.origin = data
        }

And the code in destinationView (TableViewController) is:

    var origin: String?

    override func viewDidLoad() {
     if let dataRecived = origin
     switch dataRecived {


                case "news":

                    print("News")

                case "tips":

                    print("Tips")

                case "other":
                    print("Other") 
    }

}

When I click any of the buttons, it takes me to the TableViewController, but doesn't print anything, I added the If let, since it crashed when the statement was: if let dataRecived = origin, now it never gets inside the if let statement. If I show the content of the variable (mouse over variable in xCode) it shows: ""

Any ideas why it isn't working? Not sure if it has something to do, but HomeView is a ViewController, linked to a Navigation controller that has a TableView (which is destinationView. Thanks for your help

Enrique
  • 293
  • 1
  • 2
  • 15
  • Did you check your button action outlets in the storyboard? Sometimes they can randomly become detached – Dustin Spengler Sep 03 '17 at 16:00
  • Be more specific when you say it's not working. What's it doing/not doing? Do you get an error message? – Shades Sep 03 '17 at 16:05
  • @DustinSpengler, yes they are properly connected – Enrique Sep 03 '17 at 16:13
  • @Shades It takes me to the TableViewController, but the variable doesn't have any info, if I selected in the xCode window it shows that it's "" – Enrique Sep 03 '17 at 16:16
  • You must call `super.viewDidLoad()` and since you are passing the string through the `sender` parameter you actually don't need the `buttonPressed` variable. And why do you declare `origin` as optional at all? A non-optional cannot crash. – vadian Sep 03 '17 at 16:31
  • PS: Is this the real code? The `switch` statement won't even compile. – vadian Sep 03 '17 at 16:38

2 Answers2

1

It looks like you're getting incorrect behavior because you're running the switch statement in the viewDidLoad function which happens before the data from the previous view controller gets sent.

try adding a didSet method to the origin property to load the page like this:

class tableVC {
  var origin: String? {
     didSet {
         if let dataRecived = origin
           switch dataRecived {
            case "news":
                print("News")
            case "tips":
                print("Tips")
            case "other":
                print("Other") 
          }
      }
  }

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

Or you could run the switch in the the viewWillAppear method

Dustin Spengler
  • 5,478
  • 4
  • 28
  • 36
0

I think the Issue you are having is in:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let data = buttonPressed
        if let destinationViewController = segue.destination as? TableViewController {
                destinationViewController.origin = data
        }

If you have subclassed a UITableViewCotroller, you have to cast the destination View Controller to that class

`destinationViewController as? myCustomTableViewController:

if let destinationViewController = segue.destination as? myCustomTableViewController {
                destinationViewController.origin = data
        }
Martin Muldoon
  • 3,388
  • 4
  • 24
  • 55
  • In one of the comment above Enrique mentioned that the button tap takes him to the correct view, but never update the desired info. If he were casting the next view controller incorrectly, i think it would crash. – Dustin Spengler Sep 03 '17 at 17:32
  • No Dustin. That's is not correct, because prepare would fire, but fail to execute silently and values would not be passed to the destination view controller. The segue would occur though. – Martin Muldoon Sep 03 '17 at 17:35
  • Looking at it now you're right since they're safely unwrapping the destinationViewController. Your answer is most likely the correct one in this case. – Dustin Spengler Sep 03 '17 at 17:41