Search code examples
iosuiviewcontrolleraddsubview

Parent view controller calling itself instead of child


I am (trying to) implement a main view controller container to first instantiate the introduction view controller then the actual application view controller later. But I am unable to get it to work and I feel I am missing something really basic. The introduction view controller loads in the introduction view in the initWithFrame:

Using the following interface:

@interface ViewControllerMain : UIViewController
@property (strong, nonatomic) ViewControllerIntroduction *viewControllerIntroduction;
@end

And the following implementation of

@implementation ViewControllerMain

- (void)loadView
{
    //  Instantiate the view controller for the adventure view
    self.viewControllerIntroduction = [ [ [ ViewControllerIntroduction alloc] init ] autorelease ];
}

- (void)viewDidLoad
{
    [ super viewDidLoad ];

    //  Instantiate the view controller for the introduction view
    [ self addChildViewController: self.viewControllerIntroduction ];
    [ self.view addSubview: self.viewControllerIntroduction.view ];
    [ self.viewControllerIntroduction didMoveToParentViewController: self ];
}

@end

When I step past the addSubView statement, it steps back to the addChildViewController statement.

I've been trying to work this out for hours and any help would be appreciated.


Solution

  • You should be overriding the -loadView method only if you are explicitly creating the view of your view controller (self.view). Remove your -loadView method and move the initialization of self.viewControllerIntroduction to -viewDidLoad.