Our DNA is written in Swift

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.

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.

  1. In XCode on the ressources files group choose Add – New File – User Interface / Empty XIB
  2. Open it up side by side in Interface Builder next to MainWindow.xib
  3. Drag and Drop the existing UITableView object to copy it to the new XIB and also transfer the table settings.

    Move over the UITableView

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

    Change Identity

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

    Connect 1

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

    Connect 2

  7. If there are other IBActions or IBOutlets you need to connect these as well within the same XIB.
  8. 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.

  1. Remove the UITableView from below the viewcontroller we have just created a new XIB for.
  2. 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.

    Making a Hyperlink 

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.

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.

Categories: Recipes



  1. MyAppSales 1.0.1 | Dr. Touch

Leave a Comment