views:

88

answers:

2

My app is all done and working great. So now I ran it on a old iPhone and the app takes 17.3 seconds to start!?!? i spent a lot of time looking into it and i found that the reason it is taking so long to load is i have a lot of views and each view has a png background image. All my views and made in IB and in my code:

#import "MyTestAppDelegate.h"
#import "MyTestViewController.h"

@implementation MyTestAppDelegate

@synthesize window;
@synthesize viewController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after app launch 

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}


- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}
@end

At the end of the code where is says:

[window addSubview:viewController.view];

the app seems to be loading all the views in the nib at the same time. All the png's from all the views are about 12mb. There is no need for the app to load all the views at the same time during startup.

Is there a way i can only load the first "home" view at startup? (All the views are part of the same nib.)

+4  A: 

You can try splitting your views into different nibs. Using one nib for a single UIViewController and UIView seems to be the recommended way.

If you do that, the view will only be loaded when it's first accessed.

pgb
would that be a real lot of work? like there are 30 different views in the nib.
Jeff
It may take you some time, yes, but I think it will be worth it. You can try with a few views first, until you feel comfortable to migrate more of them.
pgb
i am going to give it a shot, but I have a few questions: How can i share variables across all the different viewControllers?How do i switch between the different nid views?Thank you for your help it is greatly appreciated
Jeff
Check out the message `initWithNibName` in `UIViewController`. The docs are at http://developer.apple.com/iphone/library/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/initWithNibName:bundle:
pgb
so would i be able to only make new nib files for all my views but still use the same viewController for all of them?
Jeff
Yes, but for each view you'll create a new instance of your view controller (and use `initWithNibName`.
pgb
I am really having a week from hell. I'm sorry pgb but im just now getting this to work for me. When i use initWithNibName and use one of the new nibs that i just made all that comes up it white. You would not happen to have sample code of a multi view app with one viewController would you?
Jeff
Small snippet: ArticleViewController *articleViewController = [[[ArticleViewController alloc] initWithNibName:@"ArticleView" bundle:nil] autorelease]; [self.navigationController pushViewController:articleViewController animated:YES];
pgb
I am not using a "navigationController" I need to be able to switch views (nibs) just by tapping a UIButton.
Jeff
+2  A: 

When you load a nib, the runtime instantiates all the objects freeze dried in the nib. If you have all your views in a single nib then every single view will initialize and load even if it is not visible. Otherwise the links in the nib couldn't be resolved.

Usually, each nib should be a view/view-controller pair. Multiple view controllers can be placed in the same nib safely because they are usually relatively lightweight objects.

TechZen