Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force viewDidLoad to fire on iOS

Tags:

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)?

like image 469
Borut Tomazin Avatar asked Oct 03 '12 07:10

Borut Tomazin


People also ask

Is viewDidLoad only called once?

viewDidLoad method is called only once in ViewController lifecycle. The reason retrieveMessage() is called in viewDidLoad because it's adding observer to start listening for received and sent message.

How many times does viewDidLoad get called?

viewDidLoad() is one of the initialization methods that is called on the initial view controller. viewDidLoad() is called before anything is shown to the user - and it is called only once.

What is the viewDidLoad method?

viewDidLoad is the method that is called once the MainView of a ViewController has been loaded. This is called after loadView is called.In the image you can see the MainView and other views within it.

What is the purpose of viewDidLoad in Swift?

Use viewDidLoad( ), which is called AFTER loadView( ) has completed its job and the UIView is ready to be displayed. viewDidLoad( ) allows you to initialize properties of the view/viewController object and finalize them before viewWillAppear( ) is called.


2 Answers

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.

like image 188
Carl Veazey Avatar answered Oct 18 '22 11:10

Carl Veazey


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() 
like image 43
Rudolf Adamkovič Avatar answered Oct 18 '22 09:10

Rudolf Adamkovič