dmirkitanov
dmirkitanov

Reputation: 1396

How to instantiate a particular view controller with storyboard in iOS at early stage of loading?

When using tabs with storyboard in iOS 5, some of them may take quite a long time to initialize when switching to it (for example, a tab containing GLKViewController).

This happens because an amount of work in viewDidLoad method in this controller could be very big.

Is there a way to initialize particular view controller (and call it's viewDidLoad method) defined in the storyboard at early stage - when an application starts? Having done this, the delay should be eliminated.

Upvotes: 1

Views: 842

Answers (2)

Ash Furrow
Ash Furrow

Reputation: 12421

Are you sure it's the view controller's instantiation and not the viewDidLoad method? The view controllers are probably all created when the storyboard is unpacked, but a view controller tries to delay loading its actual view object as long as possible; viewDidLoad isn't called until the view property of your UIViewController subclass is accessed.

So a way around this could be to manually access the view property:

__unused CGRect frame = [[tabBarController.viewControllers objectAtIndex:index] view].frame;

If the slowdown is, in fact, in the instantiation and the view controller isn't being created until you switch to that tab, then you'll have do force the view controller to be instantiated by accessing it programmatically, like in the above example.

Upvotes: 1

Omar Abdelhafith
Omar Abdelhafith

Reputation: 21221

Calling the frame of the vewcontroller or the .view property will most likely work, but i dont advice you to mess up with the viewcontroller initializations and view settings

For the following reasons

  • changes you make will not be standard, they will be tricks and hacks that will later on get out of hand
  • changes that you make will not be carried with you easily to other projects you create

If i faced a problem like this i would create the GLKViewController separately for example in the app delegate and held it there, untill the viewDidLoad gets called in the viewController, then i would move this initilized GLKViewController to the viewController

Upvotes: 1

Related Questions