title: How to set a variable value in another class in Swift
body: <p>How can I add a value to a variable in a class <code>myClass2</code> from another class <code>myClass</code> in swift?</p>
  
  <p>I have already tried something like <code>myClass2().myvariable = "value"</code>, but this didn't work; my variable value is still <code>nil</code>. here's the code:</p>
  
  <p>myViewController2:</p>
  
  <pre><code>import UIKit
  
  class MyViewController2: UIViewController {
  
      var myVariable : String?;
  
      @IBOutlet weak var display: UITextField!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          // Do any additional setup after loading the view.
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      @IBAction func showPressed(sender: AnyObject) {
          display.text = myVariable;
      }
  
  }
  </code></pre>
  
  <p>ViewController:</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
      let myClassInstance = myViewController2();
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      @IBAction func toView2Pressed(sender: AnyObject) {
  
          let view2 = self.storyboard?.instantiateViewControllerWithIdentifier("view2") as myViewController2;
          self.navigationController?.pushViewController(view2, animated: true);
          myClassInstance.myVariable = "my new value";
      }
  
  }
  </code></pre>