9

Before doing any type of iOS 5.0 development using Xcode 4.2, Xcode has provided a "MainWindow.xib" in templates. After downloading and playing with Xcode 4.2 with iOS 5.0, I noticed that none of the templates provided include any "MainWindow.xib" files. Do we need this anymore? I noticed that in the "application didFinishLaunchingWithOptions" method in the App_Delegate that the templates now include code that creates some "UIWindow" and if you have any navigation controllers, it updates the window accordingly.

// code that was pulled from a Master Detail application template
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

I guess my question is, do I need a "MainWindow.xib" any longer, and if so, then why do the Apple templates exclude them?

5StringRyan
  • 3,604
  • 5
  • 46
  • 69

3 Answers3

15

It is has always been an option not to use MainWindow.xib. The UIWindow could be loaded programmatically by the doing the following:

1) Making sure that the call to UIApplicationMain in main.m looks like this:

UIApplicationMain(argc, argv, nil, @"MyAppDelegateClassName");

rather than this:

UIApplicationMain(argc, argv, nil, nil);

2) Making sure that Info.plist does not have a value of "MainWindow.xib" as the main nib file.

3) Loading the UIWindow programmatically in the app delegate.

So, no, you don't have to use MainWindow.xib and, actually, you never have. You could always do things programmatically.

It is a good question why Apple made this change in the default Xcode settings, and I don't know the answer. But it may have to with performance or the transition to ARC or small application file sizes or something else altogether.

Matthew Gillingham
  • 3,429
  • 1
  • 22
  • 26
  • Is it possible to provide a couple pros and cons? I guess I'm trying to figure out what would be the best bet moving forward regarding the usage or non-usage of the "MainWindow.xib." – 5StringRyan Oct 24 '11 at 05:03
  • I've been thinking about it, and I am actually having a problem coming up with too many pros. I suppose it allowed you to setup a tab bar based application (or maybe a navigation controller) in a little bit more of a visual fashion since you could manipulate the container view in Interface Builder. Now when I create a new tab bar application with the template, the tab bar exists only in code. But since it is still easy to create and use view controllers with Interface Builder, its hard for me to think that much has been lost. I would probably move forward without using a MainWindow.xib. – Matthew Gillingham Oct 24 '11 at 06:09
4

I think the reason why it's now absent is that creating a window programmatically is much more efficient than opening a xib file and unarchiving the objects. Generally, if you can do it in one line of code, you should do that in code.

Read Matthew Gillingham's answer for instructions on how to manually change an old project to work without the MainWindow.xib.

Costique
  • 23,712
  • 4
  • 76
  • 79
  • So do you think that just bypassing the "MainWindow.xib" is the way to go for all future projects? – 5StringRyan Oct 24 '11 at 04:49
  • 2
    Yes, I always create a window programmatically for all my projects because, you know, the first thing to appear in the window is managed by a view controller anyway. Having a xib with just a window and nothing else does not give you any advantage. – Costique Oct 24 '11 at 05:05
2

I always feel confused about the usage of MainWindow.xib: even with it I still need to set window's rootViewController or addSubview to it. Isn't that information contained in MainWindow.xib ? Why I still need to tell window about that?

So removing MainWindow.xib seems to make sense.

Qiulang
  • 10,295
  • 11
  • 80
  • 129