Quick Tip: Going completely XIB-less

Deprecation Warning

Hi there. This post was originally written back in early 2011, a long time ago. Since then the API's and methods mentioned herein have changed a lot. I wont remove this post as it still may be helpful to some but be aware that it is pretty old.

 

Transient

In some of my tutorials on MobileTuts+ I go through explaining XIB-less development and how to make your interfaces sans Interface Builder. What I demonstrated was 99% XIB-less and there was one main XIB left, the MainWindow.xib.

At the time I was unaware of a nice method of removing this XIB. However, after reading through some posts there is an easy way to get rid of that last XIB.

 

Step 1: Info.plist change

Open up the PROJECT_NAME-Info.plist in your project. Locate the "Main nib file base name" row in the dictionary and give it a good ol' delete. Save the file.

 

Step 2: Changing main.m

Next, if you are already 99% XIB-less then go ahead now and delete MainWindow.xib. If you have view controllers setup in MainWindow.xib then you will need to move them to the Application Delegate file. See Beginning iOS Development video #3 on Interface Builder for more information on how to do this, seek to 30:00 in the video and after that then delete the MainWindow.xib.

Open the main.m (in Other Sources) file in your project and find the code shown below:

And here is the code after the change:

So lets have a look at the difference. On line 6 in the code we have the UIApplicationMain method which if we look at in the documentation says:

This function is called in the main entry point to create the application object and the application delegate and set up the event cycle.

It accepts 4 arguments. The first 2 are passing the arguments through from the main() method. The last 2 allow you to specify the 'principal class name' and the 'delegate class name'. The principal class name is the name of the class that the Application Delegate inherits from, in this case its UIApplication. Finally, the delegate class name is the name of the class that we create so the Lap Timer application's Application Delegate is called 'LapTimerAppDelegate'.

Once the change is made in main.m, as illustrated above, then its time to move to the last step.

 

Step 3: Creating the window in code

Since the MainWindow.xib file is no longer available we have to create the window used throughout the application. To do this it's a simple 1 line addition to our Application Delegate.

At the top of the main application initialisation call in the Application Delegate the window is allocated and initialised. The window's frame is set as the bounds of the current device's main screen.

Check your Application Delegates header file and make sure the UIWindow property is no longer an IBOutlet.

Thats it! No more XIB's!