-2

I have a class1 which is having many variable and also having one function within a class1.I am calling the function from another class class2.function triggered but i cannot access variable of class1.

here is the sample code

class ViewController: UIViewController {

   var Flat:String?
    var Flong:String?
    var Tlat:String?
    var Tlong:String?
    override func viewDidLoad() {
        super.viewDidLoad()

     Flat = "flat value";
    Flong="flong value";
    Tlat="Tlat value";
    Tlong="tlong value";

    }

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

    func calculation()
    {
       print("origin_lat new\(Flat)")
        print("origin_lng new\(Flong)")
        print("dest_lat new\(Tlat)")
        print("dest_lng new\(Tlong)")
    }

}

I am calling calculation method from another class Collectionviewcell click function

var mycontroller : ViewController = ViewController()

  mycontroller.calculation()

Why i could not access the values anyone help me?

Hamish
  • 78,605
  • 19
  • 187
  • 280
vibin reji
  • 49
  • 4

3 Answers3

1

You can also reach other controller's variables with defining global variables like this way:

class Class1ViewController: UIViewController {
    struct GlobalVariables{
        static var Flat:String?
        static var Flong:String?
        static var Tlat:String?
        static var Tlong:String?
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        Flat = "flat value";
        Flong="flong value";
        Tlat="Tlat value";
        Tlong="tlong value";
    }
  ...
}

And you can use these variables in another view controller:

class Class2ViewController: UIViewController 
{
 ...
    print(Class1ViewController.GlobalVariables.Flat)
    print(Class1ViewController.GlobalVariables.Flong)
    print(Class1ViewController.GlobalVariables.Tlat)
    print(Class1ViewController.GlobalVariables.Tlong)
 ...
}
kkakkurt
  • 2,790
  • 1
  • 30
  • 33
1

Actually, the "viewDidLoad()" function is not called. It's will be called when you display the viewController, for example, a UINavigationController push it. In your case, you just created the viewController, not displayed it. If you want to init these variables without displaying the viewController, you need to do this:

class ViewController: UIViewController {

    var Flat:String?
    var Flong:String?
    var Tlat:String?
    var Tlong:String?

    required init?(coder:NSCoder) {
        super.init(coder:coder)
        self.customInit()
    }

    override init(nibName: String?, bundle: Bundle?) {
        super.init(nibName: nibName, bundle: bundle)
        self.customInit()
    }

    func customInit() {
        Flat = "flat value";
        Flong="flong value";
        Tlat="Tlat value";
        Tlong="tlong value";
    }

    //.......
} 
Yun CHEN
  • 6,450
  • 3
  • 30
  • 33
  • i am getting value from another viewcontroller like this http://stackoverflow.com/questions/24222640/passing-data-between-view-controllers-in-swift .So i can only intilize value only Viewdidload() – vibin reji Apr 18 '17 at 08:32
  • Once you call "[self presentViewController:...", the viewDidLoad() function will be called, then the variables could be initialized. You call calculation() after sentence "[self presentViewController:...". – Yun CHEN Apr 18 '17 at 08:47
0

What your trying to achieve is a kind of model class. I would suggest you create a simple model class which is not subclass of UIViewcontroller like so.

class Model: NSObject {
   var fLat: String?
   var fLong: String?
   var tLat: String?
   var tLong: String?

override init() {
    super.init()

    fLat = "flat value"
    fLong = "flong value"
    tLat = "tlat value"
    tLong = "tlong value";
}



// for print I have forced unwrapped so remember to check before force unwrapping smi)e
func calculation() {
    print("orgin_lat new\(fLat!)")
    print("origin_lng new\(fLong!)")
    print("origin_lat new\(tLat!)")
    print("origin_lng new \(tLong!)")
  }


}

Now inside your main View controller you can initialize and call the function like so

let model = Model()
model.calculation()
Khalid Afridi
  • 913
  • 5
  • 12