O'Reilly logo

iOS 11 Programming Fundamentals with Swift by Matt Neuburg

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7. Nib Management

In Chapter 4, I talked about ways of obtaining an instance. You can directly instantiate an object type:

let v = UIView()

Or you can obtain a reference to an already existing instance:

let v = self.view.subviews[0]

But there is a third way: you can load a nib. A nib is a file, in a special format, consisting of instructions for creating and configuring instances. To load a nib means, in effect, to tell that nib to follow those instructions: it does create and configure those instances.

My example of a UIView instance is apt, because a UIView is just the kind of instance a nib is likely to create. Nibs are edited in Xcode using a graphical interface, much like a drawing program. The idea is that you design some interface objects — mostly instances of UIView and UIView subclasses — that you want to use in your app when it runs. When your app does run, and when the moment comes where you actually need those interface objects (typically because you’re about to display them in your visible interface), you load the nib, the nib-loading process creates and configures the instances, and you receive the instances and insert them into your app’s actual interface.

In fact, because you’ll mostly be using view controllers — subclasses of UIViewController — you won’t even have to do that! View controllers already know how to work hand in hand with nibs: each of your app’s view controllers will load its own view nib and retrieve the resulting instances and put them ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required