0

I have two ViewControllers and both with their .swift class file.

The thing is i have a variable declared in the first ViewController, and it will be filled with a value entered by the user in the first view. But then i want to show that value in another view (im using a tab bar controller).

But the problem is i don't know how to reference to that variable from the SecondViewController.swift because the variable was declared in FirstViewController.swift

LPS
  • 581
  • 3
  • 8
  • 17
  • Welcome to OS! If you can provide your code, you would be able to get more help from someone. It is often hard to see what is going on without reproducible data and code. – jazzurro Sep 28 '14 at 15:33
  • I have a lot of code because im fetching the weather from a website in JSON format. But the thing is in the FirstViewController i have a textField and i want to show the value entered by the user in another ViewController. Both viewControllers are connected by a Tab Bar Controller – LPS Sep 28 '14 at 18:35

2 Answers2

0

There are two common ways to move data. If you just want instance data (the current instance of ViewController1) to move to ViewController2 you would pass the data via a segue and the prepareForSegue function. This is one of the most important thongs to learn. I will link to some tutorials below. Keep in mind, Xcode 6 has evolved and the syntax may change slightly. So for the function prepareForSegue, you need to look it up in the documentation by command-click on the word "prepareForSegue" in your swift file and it will take you to the documentation where you can cut an paste.

http://makeapppie.com/2014/07/01/swift-swift-using-segues-and-delegates-in-navigation-controllers-part-1-the-template/

http://bharathnagarajrao.wordpress.com/2014/06/17/swiftly-learning-swift/

The other common way to pass data is to have the data persist by saving to a file. Another set of links. I would also recommend going to github and search for projects with both prepareForSegue and Swift for further examples.

Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53
  • thanks for your answer. Im reading tutorials in order to use the PrepareForSegue function but the major of the examples are using a Navigation Controller but in my project im using a Tab Bar Controller. Do u know how to implement that method with a tab bar? – LPS Sep 28 '14 at 18:33
  • OK, on the tab bar when you go back and forth you open the previously opened instance of that view and do not segue to anew instance. You can use the protocol/delegate methods for sharing info, or you can save data to file and retrieve it. – Steve Rosenberg Sep 28 '14 at 18:39
0

for the protocol/delegate method of sharing data see:

https://medium.com/swift-programming/ios-swift-protocols-and-delegates-7193f7f58b8a

http://www.raywenderlich.com/75289/swift-tutorial-part-3-tuples-protocols-delegates-table-views

for saving to file using NSCoding, I posted code earlier today here:

SWIFT How to create a NSCoding Subclass and call it from another class?

If you want to save more simply with NSUserDefaults:

Store [String] in NSUserDefaults (Swift)

Community
  • 1
  • 1
Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53