0

I watched multiple videos on youtube and bought two books with explanations (O'Reilly).

It seems like a nib file is always used in one view controller. The file-owner is usually the view controller which will implement that nib.

I couldn't find an example in which a nib file is used in multiple view controllers.

I did come across a couple of posts that found ways around this but the posts are old and many people leave warnings.

My initial urge to use a nib file was because I had a table view cell that was used in multiple tableViewControllers. The tableViewCell was prototyped in one of the TableViewControllers in storyboard.

I thought that creating a nib would decouple that cell from one specific TableViewController.

I can't say that I really understand the purpose of a nib file. It seems like it's very similar to storyboard.

I ended up creating the tableViewCell in code and reusing it throughout my project.

So my question is what are nib files used for ?

Are they used to decouple a view from a specific view controller ?

Are they used to avoid duplication within one ViewController ?

Any insight would be helpful.

3366784
  • 2,423
  • 1
  • 14
  • 27

1 Answers1

0

Yes, you can reuse xibs in many places within your app.

Xibs predate storyboards and act as factories for creating instances of UIView classes configured in Interface Builder. Using them to define the layout and attributes of a UIViewController's view and its subviews is one common use case but certainly not the only one.

A common place where a xib might be reused is when it contains a table or collection view cell. You could then register that xib with multiple table or collection views using register(_ nib: UINib?, forCellWithReuseIdentifier identifier: String) to load instances of the cell when needed.

Jonah
  • 17,918
  • 1
  • 43
  • 70
  • In the example that you provided, would we then set `UICollectionViewController` as the nib file owner Type in interfaceBuilder ? Or would that be left as the default `NSObject` ? – 3366784 Jun 06 '17 at 02:03
  • There would be no reason to change the File's Owner in that case. The owner set in the xib is really just a hint for Interface Builder about what properties and selectors can be safely set as outlets and actions when an object is passed to https://developer.apple.com/documentation/uikit/uinib/1614137-instantiate – Jonah Jun 06 '17 at 04:06