I'm trying to do something a bit elaborate, but something that should be possible. So here is a challenge for all you experts out there (this forum is a pack of a lot of you guys :) ).
I'm creating a Questionnaire "component", which I want to load on a NavigationContoller
(my QuestionManagerViewController
). The "component" is an "empty" UIViewController
, which can load different views depending on the question that needs to be answered.
The way I'm doing it is:
- Create Question1View object as a
UIView
subclass, defining someIBOutlets
. - Create (using Interface Builder) the
Question1View.xib
(HERE IS WHERE MY PROBLEM PROBABLY IS). I set both theUIViewController
and theUIView
to be of class Question1View. - I link the outlets with the view's component (using IB).
I override the
initWithNib
of myQuestionManagerViewController
to look like this:- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:@"Question1View" bundle:nibBundleOrNil]) { // Custom initialization } return self; }
When I run the code, I'm getting this error:
2009-05-14 15:05:37.152 iMobiDines[17148:20b] *** Terminating app due to uncaught exception '
NSInternalInconsistencyException
', reason: '-[UIViewController _loadViewFromNibNamed:bundle:]
loaded the "Question1View" nib but the view outlet was not set.'
I'm sure there is a way to load the view using the nib file, without needing to create a viewController class.
After spending many hours, I forged out following solution. Follow these steps to create custom
UIView
.1) Create class myCustomView inherited from UIView.
2) Create .xib with name myCustomView.
3) Change Class of UIView inside your .xib file, assign myCustomView Class there.
4) Create IBOutlets
5) Load .xib in myCustomView * customView. Use following sample code.
I ended up adding a category to UIView for this:
explanation here: viewcontroller is less view loading in ios&mac
This is something that ought to be easier. I ended up extending
UIViewController
and adding aloadNib:inPlaceholder:
selector. Now I can sayHere's the code for the category (it does the same rigamarole as described by Gonso):
In swift
Actually my resolution to this problem was, to load the view in a viewDidLoad in my CustonViewController where I wanted to use the view like that:
Don't load the view in a
loadView()
method! The loadView method serves for loading the view for your custom ViewController.The previous answer does not take into account a change in the NIB (XIB) structure that occurred between 2.0 and 2.1 of the iPhone SDK. User contents now start at index 0 instead of 1.
You can use the 2.1 macro which is valid for all version 2.1 and above (that's two underscores before IPHONE:
We use a technique similar to this for most of our applications.
Barney
None of the answers explain how to create the stand alone XIB that is the root of this question. There is no
Xcode 4
option to "Create New XIB File".To do this
This may seem simple but it can save you a few minutes poking around for it since the word "XIB" does not appear anywhere.