iosuiviewcontrollerloadstoryboardtabbarcontroller

Load all UIViewController when application is launched


I have an application with a Tab Bar Controller and a lot of UIViewController, I defined the interface on storyboard, I want that when the user lunch the application (When application loaded) all ControllerViews are loaded, and not when the user select a tab (My UIViewController are complex and it take little time to load)


Solution

  • The answer you posted is incorrect; You should not call -loadView manually. From the documentation:

    Discussion

    You should never call this method directly. The view controller calls this method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property.

    So you should instead simply access the view property. Your modified code would then be:

    UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
    for(int i = 0; i < 5; i++) {
        [[tabBar.tabBarController.viewControllers objectAtIndex:i] view];
    }
    

    Also note that this work should probably not take place inside your -application:didFinishLaunchingWithOptions: method, because iOS will terminate your app if this method does not return in 5 seconds. You can schedule this task on the next runloop iteration like this:

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self preloadTabs];
    }];