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

1

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?

Furcula answered 6/2, 2012 at 4:52 Comment(0)
C
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://mcmap.net/q/65137/-can-39-t-load-uiviewcontroller-xib-file-in-storyboard-in-swift Basically this name matching was broken in iOS 8, but then Apple repented and fixed it so that it works again in iOS 9.

Construction answered 6/2, 2012 at 5:7 Comment(12)
How does this cause the view controller to load the NIB?Furcula
Just try it! Then learn how view controllers load nibs by reading my book: apeth.com/iOSBook/ch19.htmlConstruction
@Furcula 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.Lame
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?Cyanide
@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.Construction
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.Markhor
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: apeth.com/iOSBook/ch19.htmlConstruction
@Construction 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.Markhor
I got Missing proxy for identifier UIStoryboardPlaceholderForeconscious
Ok, in other storyboard it works. It mb happened because I present view controller from child view controllerForeconscious
Anyone had success using this technique in Xcode 6 and Swift? View Controller is loaded, but screen is black.Blossom
@TomekCejner I explain the reason and workaround here: https://mcmap.net/q/65137/-can-39-t-load-uiviewcontroller-xib-file-in-storyboard-in-swift Perhaps it would help if I edited my answer above to refer to that answer...Construction

© 2022 - 2024 — McMap. All rights reserved.