Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Passing Data between ViewControllers ?

So, I know how to pass data between a ViewController to the other one, but I ran into a problem with an app that I am working on.

Say that I have 3 view controllers.

ViewControllers:

  • A - B - C

So A being the main view controller has a segue that connects to B. A -> B And B has a segue that connects to C.

My goal is to be able to pass data from C to A. C -> A

Anyone has an idea how this can be done?

All of this is being done in Swift

Any input will be appreciated.

Gabe Nadel maybe you have the answer ?

4 Answers

In this case, you will have to do an unwindSegue.

In viewController A, write:

@IBAction func unwindFromViewController(segue: UIStoryboardSegue) {

        // The ViewController that is calling this segue (in this case, C viewController)
        var sourceViewController = segue.sourceViewController as! <YourViewController>

        // Now you can access the properties of your C viewController from here, and pass them to ViewController A.
}

Then, in your storyboards, click on your C ViewController, hover the mouse over your dismiss button, then right-click and drag to the "Exit Icon" (top of the ViewController), then release. You will then see the function name above in a little prompt menu, select it.

Try running your app.

So when you will pressed that button, viewController C will dismiss and you will go back to ViewController A, and the unwindSegue method will be called.

Hope this helps, good luck!

Danny Y.

Thank you for your answer, I did a solution a bit more complicated than this lol. But this looks a lot easier.

Thank you !!

Hi, your solution will be Delegates. With Delegates you can send information from one controller to another.

if have questions about that give me an email. i can show you how to achieve it.

greets Thorsten

I have delegates in my code, but for some reason my C won't pass data to A unless I have a direct segue connection to it.

A has a menu button that connects to B with a segue which B has another button that connects to C, but no direct connection from A to C. The only way for it to work its if I connect A to C with a segue. I don't want to do that since I don't want to have a button that opens viewController C from A, but I have to pass some data from C to A.

I don't know if that makes any sense lol

Here is my email jhoannarango@msn.com

Thanks for the help.

The way I solved this problem is with NSKeyedArchiver. Look that up in the documentation. What ever data I will need to use in various places in my app, I will archive it and then I can call to it from anywhere in the app at anytime. It's a really cool solution. If you want I could write out a basic code example.

Hello:

Even tho I already solved this issue, I would like to see an example of your solution.

Thank you

Jhoan Arango Think ive sent you email but not sure. Have you got email?

Hello:

No I didn't get any e-mails from you, sorry.. Perhaps re-sending it ?