0

If ViewController1 is not being presented but ViewController2 is: I can easily update GUI in ViewController1 from ViewController2 but I cannot retrieve any values in ViewController1 while ViewController2 is presented. When ViewController1 is not being presented it always returns properties as nil.

In my case, each view controller is being presented on a tab bar view controller.

What would be the best way to go about solving this?Thank you.

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
jer_francis
  • 153
  • 1
  • 11
  • 1
    what's relationship between vc1 and vc2. Your question is sort of unclear. – Lucas Huang Dec 28 '15 at 05:44
  • Check the referencing chain you set, you can update viewcontroller1 even it is under the vewcontroller2 – Jason Nam Dec 28 '15 at 05:44
  • @LucasHuang More of a general question but each view controller is on a tab bar view controller. When one is presented the other is dismissed. The dismissed view controller returns null for its properties when it is dismissed. – jer_francis Dec 28 '15 at 05:47
  • @JasonNam I know I can update one view controller from the other that isn't the problem here. The problem is getting the value of a property from the view controller that is not currently being presented. It returns nil unless it is presented. – jer_francis Dec 28 '15 at 05:48
  • how did you instantiate your view controller, segueing from storyboard or programmatically create from nib file? – Lucas Huang Dec 28 '15 at 05:50
  • @LucasHuang each view controller class has a class method that returns the instance of the view controller that currently exists. – jer_francis Dec 28 '15 at 05:51
  • Inside the class method, how did you create your view controller. I assumed you use nib file because the view controller is not fully created in that case. – Lucas Huang Dec 28 '15 at 05:52
  • @LucasHuang I created it in Main.storyboard but the instance of the actual view controller is being stored as a static variable in the class. – jer_francis Dec 28 '15 at 05:55
  • might you have to use Block on presenting viewController. i think you will find your answer here http://stackoverflow.com/questions/24603559/store-a-closure-as-a-variable-in-swift/31487182#31487182 – Jaydeep Patel Dec 28 '15 at 05:55

1 Answers1

3

The nib file you specify is not loaded right away. It is loaded the first time the view controller's view is accessed. If you want to perform additional initialization after the nib file is loaded, override the viewDidLoad method and perform your tasks there.

This is from documentation regarding to initWithNibName designated initializer :https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/initWithNibName:bundle:

EDIT based on the comment from :

If you are using UITabBarController, it has a viewControllers property so that all retain count of your view controllers is not decreased to 0 which have not been deallocated. If you found anything nil, then it should be some problems in your initializers. https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/#//apple_ref/occ/instp/UITabBarController/viewControllers

You should not have your own mechanism to access view controllers. Instead, use viewControllers property from UITabBarController because it helps managing valid view controllers.

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
  • I know but the view has already been accessed and viewDidLoad has already been executed. – jer_francis Dec 28 '15 at 05:52
  • Turns out my class method returning the instance for the view controller was nil but when I use the tab bar view controller to get it it works fine because it continues to store that instance. Thanks! – jer_francis Dec 28 '15 at 06:05
  • See my last paragraph, you should use `UITabBarController` to manage your valid view controllers. If you need to use yours, you have to be very careful how to make it parallel in both your mechanism and `UITabBarController`. – Lucas Huang Dec 28 '15 at 06:06