Search code examples
iosviewpushviewcontrollerpresentmodalviewcontrolleraddchild

What is the difference between addChildViewController and presentModelViewController


I know there are three ways to change the view in iOS

1.

[self addChildViewController:thirdViewController]; 
[contentView addSubview:thirdViewController.view]; 

2.

First * sVC = [[First alloc] initWithNibName:@"First" bundle:[NSBundle mainBundle]]; 
[self presentModalViewController:sVC animated:YES];

3.

MyViewController *sampleViewController = [[[MyViewController alloc]initWithXXX] autorelease];       
[self.navigationController pushViewController: sampleViewController animated:true];

pushViewController requires the navigation controller, which I understand. However, when to use addChildViewController and presentModalViewController??


Solution

  • These are four totally different implementations

    • addChildViewController is used in iOS5 to do viewController containment, this will enable you to easily create your own NavigationCotrollers or TabControllers its only available in iOS5

    • addSubview is the lowest level of the three, this will just add a view to another view, as a child

    • presentModalViewController is used to present a viewController modally on the screen, hence overwriting the old one

    • pushViewController used in UINavigationController to push a new ViewController to the viewcontrollers stack,