28

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?

user1175914
  • 839
  • 3
  • 9
  • 8

1 Answers1

51
  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.

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • How does this cause the view controller to load the NIB? – user1175914 Feb 06 '12 at 17:30
  • 4
    Just try it! Then learn how view controllers load nibs by reading my book: http://www.apeth.com/iOSBook/ch19.html – matt Feb 06 '12 at 19:04
  • @user1175914 The default implementation of UIViewController calls the XIB named with the class name. So if the ObjC runtime doesn't find a view controller in the storyboard, it must default to calling the NIB as usual. Or you can provide your own implementation of NIB loading by calling loadNibNamed: or something. – KPM Jul 08 '12 at 18:55
  • OK, it is work.. hmm.. but there is a new problem.. In storyboard if I want to make a button have storyboard segues, I must do ctrl+click and drag to another viewcontroller. how about with external xib?? how can I give storyboard segues to button? – user4951 Aug 29 '12 at 03:00
  • 1
    @JimThio Draw the segue from the view controller to the next view controller, and give the button a normal action. In the button's action handler, run the segue. - Either that, or don't separate the view controller's view into a separate nib as requested in the original question; just draw the view controller's view in the storyboard itself. – matt Nov 08 '12 at 02:55
  • I can't get this work for me. The MyCustomController in the storyboard (that has had it's View deleted in step 1) generates an NSInternalInconsistencyException because it's View is not set. Because there is no view in the storyboard to set it to. – Thompson Jan 19 '13 at 02:32
  • It does work, @Thompson. If it isn't working for you, you haven't configured the nib file correctly. Obviously it must have the same name as the view controller class *and* you must its File's Owner to the correct class *and* you must hook up its `view` outlet. All of that is implicit when I say "configure"! As I said in an earlier comment, read my book if you don't know how view controllers load nibs: http://www.apeth.com/iOSBook/ch19.html – matt Jan 19 '13 at 17:15
  • @matt I appreciate your help; I have followed your steps in detail but Xcode complains that nib has been named AND view has been set, and then I get the NSInternalconsistencyException. I work-around by setting `self.view = [[[NSBundle mainBundle] loadNibNamed:@"lmNumberSetView" owner:nil options:nil] objectAtIndex:0];` in the VC's initFromCoder method and I am fine, so I consider my issue resolved. I will take your advice and read your book for further study. – Thompson Jan 19 '13 at 19:03
  • I got `Missing proxy for identifier UIStoryboardPlaceholder` – Ossir Jul 24 '14 at 09:09
  • Ok, in other storyboard it works. It mb happened because I present view controller from child view controller – Ossir Jul 24 '14 at 11:03
  • 1
    Anyone had success using this technique in Xcode 6 and Swift? View Controller is loaded, but screen is black. – Tomek Cejner Sep 07 '14 at 21:16
  • @TomekCejner I explain the reason and workaround here: http://stackoverflow.com/a/25539016/341994 Perhaps it would help if I edited my answer above to refer to that answer... – matt Sep 07 '14 at 23:29