abbood
abbood

Reputation: 23548

how to make initWithNibName behave like instantiateViewControllerWithIdentifier

I'm trying to add a view controller container library to the remail email client. The idea is that the view controller container presents its child view controllers in two layers. It provides functionality for sliding the top view to reveal the views underneath it.

In the library description this is the suggested way of attaching a child controllerView to it's parent:

if (![self.slidingViewController.underLeftViewController 
        isKindOfClass:[MenuViewController class]]) {
    self.slidingViewController.underLeftViewController  = 
        [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];
  }

where slidingViewController is the top-level instance of the view controller container. With this instance, you can set the view controllers underneath the top view and add panning.

I'm using xib files, not a storeyboard. so my code looked like this instead:

if (![self.slidingViewController.underLeftViewController 
        isKindOfClass:[MenuViewController class]]) {
    self.slidingViewController.underLeftViewController  =
         [[MenuViewController alloc] initWithNibName:@"Menu" bundle:nil];
}

but using that code.. I'm getting this error:
-[__NSArrayM insertObject:atIndex:]: object cannot be nil

which can be traced to slidingViewController doing the following:
[self.view insertSubview:_underTopViewController.view atIndex:0];

looking at the documentation.. I see there is a difference between instantiateViewControllerWithIdentifier and initWithNibName: the former returns an object at all times.. where as the latter only loads the first time the view controller’s view is accessed.

Question: how do I make an initWithNibName return a loaded viewcontroller object regardless if that view has been visited or not.. similar to instantiateViewControllerWithIdentifier?

Upvotes: 0

Views: 1077

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 180867

You should be able to trigger it by just accessing the view property, something like;

if (![self.slidingViewController.underLeftViewController 
              isKindOfClass:[MenuViewController class]]) 
{
    MenuViewController *vc = 
      [[MenuViewController alloc] initWithNibName:@"Menu" bundle:nil];
    [vc view];  // <-- access the view and trigger loading
    self.slidingViewController.underLeftViewController  = vc;
}

Upvotes: 1

Related Questions