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??
These are four totally different implementations
addChildViewController
is used in iOS5 to do viewController containment, this will enable you to easily create your ownNavigationCotrollers
orTabControllers
its only available in iOS5addSubview
is the lowest level of the three, this will just add a view to another view, as a childpresentModalViewController
is used to present a viewController modally on the screen, hence overwriting the old onepushViewController
used inUINavigationController
to push a new ViewController to the viewcontrollers stack,
1) was introduced in iOS 5 as part of Apple's paradigm shift to allow view controller hierarchies, it just puts a view controller in front of the current one. You have to manage the flow of controllers.
2) Is the same as one, except it can only be done for one view controller at a time. Actually, this method has been superseded by [self presentViewController:animated:completion:]
3) Adds the view controller to a list so you can go back to the previous one after hitting 'back'. iOS will manage the flow of controllers for you.
来源:https://stackoverflow.com/questions/11186758/what-is-the-difference-between-addchildviewcontroller-and-presentmodelviewcontro