XIB Lazy Loading – Part 1

3 Months ago, when I started developing MyAppSales I felt confused by multiple XIB files, so I decided to put all view controllers into the MainWindow.xib. Not only does this not really gain you more overview, but this practise slows down application launch. On the iPhone you should defer work whenever possible.

That’s why I revisited my XIB files for version 1.0.1 and moved parts to external files wherever possible. Here are all the steps.

BSA Banner

From this image you can see the structure of my MainWindow.xib. You have a UITabBarController at the root. This has 4 pages i.e. four UINavigationControllers. And each of these has my sub-classed UITableViewControllers.

First we need to move the customized view controllers to their own files: AppViewController, ReportRootController, ChartRootController and SettingsViewController. In this article I only show ChartRootController, the others where dealt with the same way.

Drag and Drop the existing UITableView object to copy it to the new XIB and also transfer the table settings.

Change the “File’s Owner” class from NSObject to ChartRootController.

Connect the view Outlet of “File’s Owner” to the table view

Connect the dataSource and delegate outlets of the table view to “File’s Owner”

If there are other IBActions or IBOutlets you need to connect these as well within the same XIB.

Save and close, that’s all it takes for the new XIB.

Next we need to remove the table view instance we just copied to a new file from the MainWindow.xib and replace the previous custom view controller with a reference to the new XIB.

Remove the UITableView from below the viewcontroller we have just created a new XIB for.

Make the viewcontroller into a link by entering the new XIB file’s name. You’ll see that instead of the generic table place holder it now says “View loaded from ‘ChartRootController’ and a blue hyperlink. By clicking on this hyperlink you open the linked XIB.

You now have done open heart surgery, namely a four times tripple bypass. So let’s see if the app still works. Check if all tabs still show something and if the buttons in the task bar are still working.

Yes it does still work! The patient is still alive.

Next time we have the stomach for such gruesome surgery we’ll look into moving those 4 navigation controllers also into external files. This might gain us even more perceived performance since at launch only one of the tabs is visible. The other three can be loaded later.