15

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.

diederikh
  • 25,221
  • 5
  • 36
  • 49
niels
  • 185
  • 1
  • 2
  • 9
  • May be error is in your overrided initWithNibName method – oxigen Jul 01 '09 at 11:44
  • It's just the default: - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { // Custom initialization NSLog(@"In pageviewcontroller custom init"); } return self; } – niels Jul 01 '09 at 16:33

1 Answers1

17

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 its view to the view you have in Interface Builder.

John Topley
  • 113,588
  • 46
  • 195
  • 237
drvdijk
  • 5,556
  • 2
  • 29
  • 48
  • 2
    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 Jul 01 '09 at 16:47