How to instantiate a view from a xib file

Dowanna
3 min readJul 12, 2018

--

As explained in this previous article, storyboards may not be the best solution when developing as a team. But without storyboards, how do you instantiate views? This is the topic I will be covering in this article.

How to instantiate a view from a xib file

  • Create a “Xibview.swift” file. This will be a convenience class capable of creating a view from a xib file. Every UIView we create from now-on will implement “Xibview”.
  • “initViewFromName” is responsible for creating the view. It will…
  1. Receive a UIView class, and extracts its classname.
  2. Generate a nib from the classname.
  3. instantiate a view from the nib.
  • For those wondering how nib / xib differ, they are functionally identical. Nibs are binary files, while xibs are in xml format, so xibs are preferred when integrating a version management system such as GitHub.
  • Create a xxxView.swift, which will be the file-owner of your xib file (which we create in the next step). The classname doesn’t matter, but make sure it inherits XibView, the class we just created above.
  • Create a xib file. Specify the class we created above as the File’s Owner. The file’s owner is (usually) the class used to access the xib file in runtime. It may be a good idea to change the background color so you know when the view is properly displayed
  • BE AWARE! Make sure the “xib file” and “classname” have identical names. Why? Because XibView (which we created) is only capable of instantiating a xib file from its classname.

Preparation finished!

  • That’s all! XibView will take care of all the instantiation and adding a full-size view to your ViewController. Here’s an example of how you might create this view:
  • When initializing this ViewController, it also initializes the view we created. The view initializer is not written in the class itself, so XibView will handle it.
  • By setting self.view within loadView(), we tell the ViewController which view to present.

Some Pitfalls

There are some pitfalls when creating a view from a xib. For example…

This is a bad idea. Can you guess why?

Well, let’s see what happens if we run the program.

EXC_BAD_ACCESS…. this doesn’t help much, but take a look at the stack trace. We have a neat infinite loop!!

When the “View” itself (instead of the file’s owner) is assigned a custom view class, the init code in the custom view class is invoked upon every xib instantiation.

But within the init code, we are instantiating a new view from a xib.

When instantiating this xib, our custom view’s init code is invoked.

But within the init code, we are instantiating a new view from a xib.

When instantiating this xib, our custom view’s init code is invoked.

But within the init code…. you get the rest.

Summary

In this article I explained

  • how to create view from a xib file
  • how to attach the full-size view to a ViewController
  • some pitfalls :)

--

--

Dowanna

CEO of Praha Inc., an IT startup in Japan. Also an iOS / web-front engineer. https://praha-inc.com