Problem passing data when I use Navigation Controller "Show" segues

Hi, I am a newbie coder and almost finished my first app but I have run into a snag which I am hoping the Community can help me with. I apologise in advance for this long message.

My app (intended to be used on all models of iOS devices) originally used buttons on each View Controller to navigate between them with each subsequent view presented modally. This worked well for nearly all my simulated devices with the exception of the smaller screen sizes. I had great difficulty fitting in all the necessary elements on each view taking into account the space needed for the buttons. I therefore removed the buttons, embedded my first VC in a Navigation Controller and also added a toolbar with toolbar items linked to “Show” segues between the views.

However, my second VC has user input text fields with global variables which need to be passed to VC9 (the report VC) but since my change in Navigation method this data is no being longer passed.

I suspect this may be due to the “Show” segue rather than the modal method used previously. Alternatively, If I keep the Nav Controller but change the segues to modal presentation I lose the appearance of the Navigation Bar and Toolbar and more importantly the data still doesn’t pass to VC9!

The Segue from my input VC2 has an identifier but I do not see how this is used with Global Variables.

As a test, I added a name text string (mimicking an input from the user) directly into the “name” global variable (before the UIViewController Class) and this does indeed pass to VC9.

The only difference I can find in my code for this data input VC is that the IBAction button is not linked to the “Submit” button itself when using the “Show” segue. Is it possible to have 2 links - one from the button to the code and the other from the button to the next VC creating the “Show” segue?

Any advice or help will be gratefully received!

Thanks,
Rhys

Hi @Rhys,

When using segues to transition from one VC to the next you should use the override method as per the sample that follows. In that you test to identify the segue identifier and create a constant as a pointer to the VC you want to go to. Then you specify the property in the destination VC that you want to pass a parameter to and assign that to the local property you want to pass. You can pass multiple properties.

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "DetailView" {
            let destVC = segue.destination as! DetailViewController
            destVC.passedText = "Segue from Home VC"
        }
    }

Hi Chris,

Thank you once again for your help and for the example code which I incorporated into my app. I am pleased to say that my data is now correctly passing to the required ViewController. :grinning:

@Rhys
That’s awesome Rhys.