-3

I have a UIViewController which loads a subview from a nib and adds it to a subview in its view hierarchy. Now this is working all fine in the iPhone story board, but in the iPad one although I do see the view but as the viewWillAppear is not called the UIImageView on the view is not initialised.

Here's the code from the main viewcontroller (the one that loads the subview)

SubViewController *controller = [[SubViewController alloc] init];
NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:@"x"] owner:controller options:nil];
UIView *subView = bundle[0];
[self addChildViewController:controller];

if (isPhone)
{
    CGRect frame;
    frame.origin.x = 0;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    view.frame = frame;
    [self.scrollView addSubview:view];
 }
 else
 {
     view.frame = self.viewSlot.frame;
     [self.viewSlot addSubview:view];
 }

The only difference is on the iPhone it is added to a UIScrollView whereas on the iPad it is added to a UIView.

viewSlot and scrollView are both outlets which are properly initialised and work. Even on the iPad I do see my view (I've changed its background colour) it's only the initialisation (viewWillAppear) that does not run.

Janos
  • 1,987
  • 3
  • 17
  • 24
  • 1
    Put your relevant code here :) – iPatel Feb 18 '14 at 12:22
  • YOu mean to say you are adding a 'viewcontroller.view' as a subview of other viewcontroller? – Rajesh Feb 18 '14 at 12:28
  • 1
    let's read tea-leaves – Injectios Feb 18 '14 at 12:28
  • As others have stated, you need to post the relevant code. Given your claim to be adding views (as opposed to view controllers), the mysterious part is actually the fact that `viewWillAppear` is being called on iPhone, not that it's not being called on iPad. – nhgrif Feb 18 '14 at 12:31
  • Guys, you are right, this question was half cooked. I've added some code, hope it's ok now. – Janos Feb 18 '14 at 20:20
  • A similar problem: http://stackoverflow.com/questions/18235284/uiviewcontroller-viewwillappear-not-called-when-adding-as-subview. As for now I haven't found any better solution than calling viewWillAppear manually. Which is strange as this is working on the iPhone. – Janos Feb 19 '14 at 06:22
  • Also checked `viewDidLayoutSubviews` not called either (called only on iPhone). This is interesting, as this is something that is a documented technique. And actually quite useful, as you may want to assemble views out of smaller subviews occasionally. – Janos Feb 19 '14 at 06:34

1 Answers1

0

After a bit of playing I've managed to get to the bottom of this. First of all I chose to initialise the controller in a different manner:

SubViewController *controller = [[SubViewController alloc] initWithNibName:[@"X" stringByAppendingString:idiom] bundle:nil];

I've got an exception that revealed the problem: the view outlet wasn't set. Lesson of this story: don't just alloc - init a controller, use the initWithNibName initialiser. It's still weird that due to some not yet discovered difference between iPhone/iPad the earlier approach still worked on the iPhone.

Janos
  • 1,987
  • 3
  • 17
  • 24