28

I have two window app and while I present first window I would like the view in second window to load and prepare content for later in background.

I've tried to use method loadView but Apple says you should not call this method directly.

So far I've chosen to use the view's method userInteractionEnabled which actually implicitly calls viewDidLoad method.

Is there an elegant way to force ViewControllers viewDidLoad method to fire before it should naturally (when window is key and presented)?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Borut Tomazin
  • 8,041
  • 11
  • 78
  • 91

4 Answers4

58

You can just call [viewController view];.

The documentation for UIViewController explains how the view property is lazy-loaded and that viewDidLoad is called after the view is loaded.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
  • 6
    So simple yet so powerful :) Thanks! – Borut Tomazin Oct 03 '12 at 07:30
  • 1
    Nice! At first I tried viewController.view but it gave a warning but sending it the message "view" this way doesn't give a warning at all – Korey Hinton Dec 18 '14 at 13:57
  • This was exactly what I was looking for, but is there a documentation/explanation on how this works and what it does? – Spartacus9 Apr 06 '17 at 14:00
  • 1
    @Spartacus9 Yes the [documentation for `UIViewController`](https://developer.apple.com/reference/uikit/uiviewcontroller/1621460-view?language=objc) explains how the `view` property is lazy-loaded and that `viewDidLoad` is called after the view is loaded. – Carl Veazey Apr 06 '17 at 15:15
34

In iOS 9, Apple finally fixed this:

// Loads the view controller's view if it has not already been set.
@available(iOS 9.0, *)
public func loadViewIfNeeded()
Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118
6

Swift 2.0, with same result of @Carl Veazey's solution:

let viewController = MyCustomViewController()
_ = viewController.view
Thomás Pereira
  • 9,589
  • 2
  • 31
  • 34
  • I am trying this with swift and it's not working... Did this stop working with the newer versions of iOS/Swift? – Luccas Correa Apr 28 '16 at 17:03
  • What do you mean with "newer versions of iOS/Swift"? Swift 3.0? I didn't test Swift 3.0 yet, so I don't know If this approach still works. – Thomás Pereira Apr 28 '16 at 17:40
  • I am using swift 2.2, and this is not working for me... The only difference between my code and yours is that instead of instatiating the View Controller myself, I am calling the method instantiateViewControllerWithIdentifier of the storyboard instance. – Luccas Correa Apr 28 '16 at 17:44
  • 3
    My bad, I was instantiating a NavigationController, that's why it wasn't working... Thank you – Luccas Correa Apr 28 '16 at 17:51
  • 1
    Ok, no problem. I've just test and confirm that It still works! ;) – Thomás Pereira Apr 28 '16 at 18:01
0

You can create a global instance for that controller(May be in AppDelegate) and call the method you want to perform the action for. Then While pushing to that controller don't create a new instance Just use the instance you have created for global use.

Suresh Varma
  • 9,750
  • 1
  • 60
  • 91