Are nib/xib files meant to be used in diffrent view controllers ?

41 Views Asked by At

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.

1

There are 1 best solutions below

2
Jonah On

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.