1

This is a root viewController. Is it possible to access to this variable from appDelegate class?

I mean inside AppDelegate: self.window.rootViewController.somethingToDo()

The problem is that is I attach this object using a @IBOutlet it seems this variable will be initialized at viewDidLoad() state. This is a too late state.

Is there any other method to access?

enter image description here

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • Are you trying to access the rootViewController from another viewcontroller? – Socram Sep 26 '18 at 17:06
  • @Socram from the appdelegate – Vyacheslav Sep 26 '18 at 17:06
  • And your problem is that the object "Application Initializer" (that I suppose is your app delegate) only gets initialized on viewDidLoad() (since it is an IBOutlet) and you want to access it earlier. Is that right? – Socram Sep 26 '18 at 17:23
  • @Socram yes, i just want to know whether is information available somewhere outside of iboutlet – Vyacheslav Sep 26 '18 at 17:36

1 Answers1

0

Yes, you can access the rootViewController from the appDelegate as you stated.

If you want to access the appDelegate earlier than viewDidLoad() you can always override one of the standard init() methods of the UIViewController and access the appDelegate like it is recommended the following question:

How do I get a reference to the app delegate in Swift?

If, for instance, you are initializing the UIViewController from the storyboard, you can override the init?(coder aDecoder: NSCoder) in your custom UIViewController class:

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    // get the appDelegate here and do your magic
}

Hope this helps.

Socram
  • 165
  • 1
  • 6