DKW DKW - 2 months ago 16
iOS Question

iOS Swift - Call function for another class and see null variables

i have three classes, where i have a container, mainview and a left menu.

App looks like this

When i press a button in the left menu, this execute an action in mainview:

var viewController: ViewController = ViewController()

func menuLeftButton1(){

But, when i want to change a variable of the mainview in that function in debuger says that the varieble of maninview is null.

I think that the variables of the mainview are destroyed and i can't use them when i return from the left menu.

Other case is when i have a switch in the left menu and I enable it
and close the left menu and open again the switch looks like I haven't
enable it.

Can u help me with this case? Thanks.

Answer Source

I think problem is in this code:

var viewController: ViewController = ViewController()

You need to instatiate viewcontroller from a xib or storyboard

For storyboard you can do this:

You need to set the identifier of the view controller in storyboard

let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewControllerIdentifier")