Combining Storyboards and XIBs in Xcode

Posted on 2013-11-12 17:22:35+00:00

You're zipping along prototyping your app using Interface Builder and storyboards. But now you want to reuse a scene all over the place. What do you do? Combine the power of storyboards and XIBs by mixing them!

You could segue from many different areas, but unless the view controller behaves quite similarly, you'll soon run into issues with IBOutlet and IBAction mappings. The best solution? Use a XIB for those tricky areas.

Learning iOS? Subscribe to my iOS articles, tips and tutorials.

You could also have the same Controller.h and Controller.m files, but build the scene in Interface Builder multiple times. But this is a pain to maintain and error-prone.

How do we do this? Quite simple actually. As always, a few assumptions first:

  1. We're using a UITableViewController somewhere in the storyboard.
  2. When we click on a cell of the above table, we want to segue to our custom view controller. However, don't set up any segues related to this yet.

First off, create your new view controller, making sure it subclasses UIViewController and that you tell Xcode to use a XIB. Let's call it CustomViewController. Feel free to add buttons and labels to your XIB. Connect outlets and actions as necessary.

Now head over to your main storyboard. Add an empty UIViewController onto the canvas and change the identity to CustomViewController. Now here's the key part: using the document outline1, find the View listed under Custom View Controller. Highlight it, and delete it.

And that's it! Now the storyboard will load your custom XIB whenever it segues to this view.

So what are the advantages? You can create segues back from this view to other parts of the storyboard. Granted, you'll have to call them manually, but this is a huge advantage.

Try it: control-drag from your placeholder controller in the storyboard back to the UITableViewController. Highlight the created segue, and in the Attribute Inspector, rename it to backToTable. Then in an IBAction method inside CustomViewController, call:

[self performSegueWithIdentifier:@"backToTable" sender:self];

Moreover, if all these controllers are embedded in UINavigationController, you'll get all these segues for free!


  1. That's the side bar on the left when using Interface Builder. 

Comments

Pieter E.2014-06-06 17:40:04+00:00

Thank you for this great blog post. I tried your solution and it works excellent when using one single xib file. But adding multiple Views, based on your blog post, crashes the application every time. Do you have any idea how to solve this? Thank you in advance for your response.

Reply
silviogutierrez2014-06-09 20:32:46+00:00

I'm afraid I'm not sure how to address this. Thanks for reading.

Reply
Badru2015-03-16 09:59:33+00:00

Thanks.

Reply
Alex Zak2015-06-23 12:52:35+00:00

Hey, I tried to follow your steps, but it seems like storyboard in not loading my VC's xib, but rather showing an empty, transparent view... did you ever encounter such a behaviour? any suggestions?

Reply

Post New Comment