views:

7535

answers:

2

I have subclassed UIViewController into a new class, PageViewController (I'm writing a simple book app). I want to add a new view loaded from a nib file and am using the following code. It works.

PageViewController *viewController1 = [[UIViewController alloc] initWithNibName:@"Page1" bundle:nil];
[viewController1.view setUserInteractionEnabled:YES];
[self.view addSubview:viewController1.view];

However, the first line is wrong because I should be calling alloc on PageViewController. When I correct it (below), the code compiles but the xib file doesn't load and the view is just transparent.

    PageViewController *viewController1 = [[PageViewController alloc] initWithNibName:@"Page1" bundle:nil];
[viewController1.view setUserInteractionEnabled:YES];
[self.view addSubview:viewController1.view];

The PageViewController initWithNibName method has been uncommented and is just the default, setting self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil].

What I have tried: In the Page1 nib file, I have tried changing the File Owner class between PageViewController and UIViewController. Yes, I remembered to connect it back to the view outlet afterwards.

Help, please! I am stumped.

+3  A: 

Have you overridden the loadView method in PageViewController? What if you NSLog the viewController1.view ?

Indeed, in Interface Builder, you have to set the file's owner to PageViewController, and connect it's view to the view you have in Interface Builder.

drvdijk
THANK YOU! The bug wasn't in loadView, it was in viewDidLoad - I had some code in viewDidLoad in PageViewController that was overwriting the nib file initialization. I spent last night from 2am to 4am trying to track that down, thanks for the help!
niels
A: 

Just I had the same problem and your advice above let me fix it after only having failed for 30 minutes! Thanks so much! I changed the class of my nib from NSObject to the correct class. Then in 'Connections Inspector' did a drag from 'view' to 'Files Owner'. Thanks again!

James Barry