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

Sending data between ViewController

Hi! I got a question. Im using Swift and Xcode 6... I would like to have 4 buttons in the First Viewcontroller. Each button will display their own label text in the Second Viewcontroller. Hope you guys understand. I can't find any tutorials on youtube?

//Jonas

3 Answers

i use objective-c so i'm not sure this will work for you but incase you need to pass data from the second view controller to the first one we use delegates so you may search for tutorials like "passing data between view controllers using delegates in swift"

Ive seen some tutorials about sending data between view controllers, but none of them learns me how to make each button, their own label text?

Let me make an example... If i press Italy in the first view controller, the label text will be "Rome" in the second view controller. If i press Spain in the first view controller, the label text will be "Madrid" in the second view controller. And so on...

ok you can create a variable called btnPressed for example When the italy button is pressed,btnPressed variable is set to 0 when the spain button is pressed,btnPressed variable is set to 1 then in your prepareforsegue method check if the btnPressed is 0 then pass the value you need (Rome) to the second view controller else if the btnPressed is 1 then pass (Madrid)