Removing default window nib in a window based application

When creating a window based application in Xcode4, it usually create an unwanted nib file that represents the default clear white window.

Note: your class names will probably be different then mine, try to keep up, its not that hard.

Some times we want to remove it and replace it with are own view controller, here is how:

  1. delete the default MainWindow.nib. You can totally delete it, assuming you have no information in it.
  2. Click on the project (above the files – blue strip) and select the target. on the right, go to info, locate the “main nib file base name” line and remove it entirely. You can run your application now with no errors (and no output…).
  3. I assume that you have created a view controller. if not create it now. Go to your app delegate file header and import the header for your master view.
  4. declare a property for that controller and synthesize it on the m file.
  5. now we need to create the default window: self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; add this to the application didFinishLaunchingWithOptions function. BTW – the reason that we didn’t get an exception so far is because once the main nib is deleted, the entire application function will not be called.
  6. next, create the nib file: mainController=[[SwitchViewController alloc]initWithNibName:@”MainView” bundle:nil];
  7. then, assign the master controller to the window: [self.window setRootViewController:mainController];
  8. Now, the application is not even aware at this point of its delegate. This is bacause we delete the main nib file, so changes must also be done to the main.m file, hiding sometimes in the supporting files folder. this is actually very simple – just feed the UIApplicationMain function the delegate it deserves! So, instead of the default UIApplicationMain(argc, argv, nil, nil); replace the last nil with your class delegate name, like so: UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

Now it’s working

NSStringFromClass gives a string… from the class. which is what this function requires, in case there is no nib file that loads that data for us.


Leave a Reply

You must be logged in to post a comment.