search by tags

for the user

adventures into the land of the command line

passing variables between view controllers and storyboards with swift 2

I discovered three ways to do this, but there are probably more.

Technique 1. Between View Controllers

Make sure you have a segue between two view controllers and the segue has a name. Option drag the mouse to create a segue between two Views and then select the segue and its Attribute Inspector tab. You’ll see some field named “Identifier” and you’ll want to add a unique identifier for it. Let’s call this identifier “someSegueID”

In the first view controller’s class, right at the bottom after viewDidLoad(), place this code.

class 1stViewController: UIViewController {

  override func viewDidLoad() {
  super.viewDidLoad()
    .
    .
    .
  }

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "someSegueID"){
        let myDestinationViewController = segue.destinationViewController as! 2ndViewController
        myDestinationViewController.variable1 = "some string"
        myDestinationViewController.variable2 = 123
        myDestinationViewController.variablex = someDict["someKey"]
    }
  }

}

In the second view controller’s class make sure the variables you are passing to it are declared right at the top of the class.

class 2ndViewController: UIViewController {
        var variable1:String!
        var variable2:Int?
        var variablex = [AnyObject:AnyObject]()
}

Technique 2. Between Storyboards

You can put this anywhere in your code you want the app to move to your other view in another storyboard.

let storyboard = UIStoryboard(name: "Transactions", bundle: nil)
let myView = storyboard.instantiateViewControllerWithIdentifier("secondView") as! 2ndViewController
myView.variable1 = "some string"
myView.variable2 = 123
myView.variablex = someDict["someKey"]
self.presentViewController(myView, animated: true, completion: nil)

And similarly, make sure the second view controller has variables declared.

class 2ndViewController: UIViewController {
        var variable1:String!
        var variable2:Int?
        var variablex = [AnyObject:AnyObject]()
}

Technique 3. Global variables

Ughh favourite way, no fuffing about. Somewhere, maybe in a dedicated file for all of your global variables, create them like this.

import UIKit

public var variable1:String!
public var variable2:Int?
public var variablex = [AnyObject:AnyObject]()

class class myViewController: UIViewController {
      .
      .
      .
}

Now you can use these suckers from anywhere without having to override anything. Just keep in mind though that if you re-declare these in another class, the class will know about both of them.

public var variable1:String!

class class myViewController: UIViewController {
        var variable1:String!
        self.variable1 = "hello"
        variable1 = "world"

        print(self.variable1)

}

variable1 will hold the string “hello” when printed.