Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using XCode storyboard to instantiate view controller that uses XIB for its design

I have a UIViewController subclass that I created previously that had its controls laid out in a XIB file.

I'd like to use this view controller in a storyboard now, but it seems although I can indicate a view controller's classname in the storyboard, I can't tell it to load the XIB.

I'd rather not move everything from the XIB to the storyboard but keep it in its separate XIB.

How can I get this UIViewController in the storyboard to load my XIB?

like image 981
user1175914 Avatar asked Feb 06 '12 04:02

user1175914


People also ask

Should I use XIB or storyboard?

BUT, if you a professional developer, you MUST use xibs for VCs, Cells, etc, Because this is the only way to make them really reusable and portable, in order to gain so much time for the next project, or even when having to make a little change or build the storyboard again.

How do I add navigation controller to XIB?

Opening a View Controller in a XibFrom the drop down menu select Editor>Embed in > Navigation Controller. We need another view controller as a destination. Press Command-N or click File>New>File… Choose a iOS source template of Cocoa Touch Class.


1 Answers

  1. Delete the View contained by the view controller in the storyboard.

  2. Then provide the view by configuring a nib file with the same name as the view controller class. For example if the view controller class is called MyViewController, name your xib file MyViewController.xib.

EDIT Note that Swift seed 5 started breaking this technique, because it mangles the name of the .xib file it's looking for. See my answer here for workarounds: https://stackoverflow.com/a/25539016/341994 Basically this name matching was broken in iOS 8, but then Apple repented and fixed it so that it works again in iOS 9.

like image 113
matt Avatar answered Sep 27 '22 20:09

matt