UINib (iOS): possible to instantiate more than one view from a nib file?

335 views Asked by At

I noticed that UINib:instantiate(withOwner:options:) returns an array. Does this mean that one nib file can contain more than one view? In every example I saw there was only one view, which was the first object in the array. What are the other (possible) elements in the array?

I'm asking because I'm trying to understand how this whole mechanism work, not so much because I want to work this way, so any theoretical background info will be welcome.

Thanks!

EDIT: if I can have more than one view in a nib file, how do I connect the second to a UIView subclass? There's only one File's Owner.

1

There are 1 answers

2
Bilal On

Yes, You can have multiple views in nib file. For example.

A Test.nib with two root views. enter image description here

Load it

if let views = Bundle.main.loadNibNamed("Test", owner: nil) {
     print(views)
}

views will be an array of two. First element of array will be UIView and second will be UITableViewCell

enter image description here