Search code examples
iosios5uiviewcontrollerstoryboarddelay

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.


Solution

  • 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.