0

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.

Yotam
  • 9,789
  • 13
  • 47
  • 68
  • The [Resource Programming Guide](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW8) has some useful info. – pbasdf Jan 15 '18 at 09:36
  • Already read it. Could not find an answer to my question, and the language is far from simple. – Yotam Jan 15 '18 at 09:40

1 Answers1

1

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

Bilal
  • 18,478
  • 8
  • 57
  • 72
  • So, what if I want to connect them to a subclass of UIView? Is that possible? There's only one file's owner – Yotam Jan 15 '18 at 09:41
  • You can set the custom class in `Identity Inspector` for both views. – Bilal Jan 15 '18 at 09:43