0

I pretty doubt that what is different between value by using prepare func

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "MainToTimer") {
        let vc = segue.destination as! YourViewController
        vc.var_name = "Your Data"
    }
}

or declare global variable for example

in VC1

var justsimpleint:Int! = 0 //out side class

viewdidload(){
 justsimpleint = justsimpleint + 2
}

in VC2

viewdidload(){
print(justsimpleint)   // it will be 2
}
Eric Aya
  • 68,765
  • 33
  • 165
  • 232
Joey
  • 351
  • 1
  • 4
  • 18
  • Refere here, https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/index.html – Rajamohan S Nov 19 '17 at 10:52
  • @RAJAMOHAN-S this is a *Swift* documentation link, it has nothing to do with working with iOS, passing data between view controllers. – Ahmad F Nov 19 '17 at 10:56
  • What is the question? A simple google search "Passing data between view controllers" will yield plenty of results. see [how to ask good questions](https://stackoverflow.com/help/how-to-ask) – RLoniello Nov 19 '17 at 11:54
  • @AhmadF , View controllers are classes. – Rajamohan S Nov 19 '17 at 13:28

2 Answers2

1

By default, assuming that you are working with storyboards, you should use the segues for passing data to the next view controller.

Global variable would be useful in case of you want it to be shared in the whole application, you could create a Singleton class for setting global properties.

Also, for passing back between view controllers, you could create a delegate to achieve it, you might want to check Passing data back from view controllers Xcode.

If you are unfamiliar with work with delegates, you might also want to check this answer.

Ahmad F
  • 26,570
  • 13
  • 76
  • 124
1

Using global variables to pass data between VCs is not a good idea. Global variables are things that should be used carefully. If you want to pass data from a View Controller A to View Controller B, which A presents, do it in the prepareForSegue method. If you want to pass data from View Controller B to View Controller A, which presents B, use the delegate pattern.

Global variables are bad for this purpose because:

  • They can be accessed from anywhere. This makes it easy for you to change them accidentally.
  • It is easier to spot errors. Say you have a global variable val that VC A and B will use. You pass some data from A to B. The first time A presented B, the data was passed successfully. The second time, the data was not sent due to some error in your logic. This would mean that B will receive the data you sent last time and everything will seem OK from the outside. If you pass the data in prepareForSegue, VC B will not get the data if no data was passed. A likely unexpectedly found nil while unwrapping optional error will occur. This makes it easy for you to tell what went wrong.
  • Global variables will hold references to objects, even when the VC that needs it is deallocated. This means that there will be some useless objects floating around in memory after the VC is dismissed if you don't clean it properly.
Sweeper
  • 145,870
  • 17
  • 129
  • 225