Take the 2-minute tour ×

I create a ViewController named "YLJTestViewController" by interface builder ,code is like:

    YLJTestViewController *testViewController = [[YLJTestViewController alloc]initWithNibName:@"YLJTestViewController" bundle:nil];
    [self.navigationController pushViewController:testViewController animated:YES];
    //[self.view addSubview:testViewController.view];

but when I use [self.view addSubview:textViewController.view];it crashed,but use [self.navigationController pushViewController:testViewController animated:YES];it works well,so what's the difference?I thought they are the same...

share|improve this question
add comment

5 Answers

-addSubview: is a method of UIView. It inserts a view into another view. Like adding a button on a page.

-pushViewController: is a method of UINavigationController. It pushes a view controller onto a navigation stack. Like sliding from a table view to a details view.

In short, -addSubview: composes a view. -pushViewController: is a transition between views.

share|improve this answer
add comment

pushViewController is like adding a piece of paper onto a stack of paper, while addSubView is like gluing a piece of paper onto another paper.

There is no explicit relationships between the previous view and the new view of the view controller which is pushed (like the pieces of paper are still separated in the stack). While the parent view will keep a strong reference to its subviews (like glue).

share|improve this answer
add comment

As sptrakesh states in this Apple Support forum thread:

addSubview is a lower level feature, which you use to add additional views to your parent/main view. pushViewController replaces the current main view in your window with the view associated with the new view controller. You use presentModalViewController when you want to display a view modally (blocks previous view) on top of your current view. If you use full screen for your modal view controller, there is not too much difference between pushViewController and this in terms how the UI behaves. When you use pushViewController you can "pop" to any view controller in the array of view controllers that have been pushed, which is not as easy to do with nested modal views.

share|improve this answer
If you copy language from other source, you must properly cite them. I've done that here, but please do this in the future or this will be considered plagiarism. –  Brad Larson Aug 28 '12 at 18:17
add comment

In your case the problem is not the use of addSubview: vs. pushViewController:animated:, but simply a typo when you use addSubview:.

[self.view addSubview:textViewController.view]; // misspelled

Should be (replacing x with s)

[self.view addSubview:testViewController.view]; // correct

As for the difference between addSubview: vs. pushViewController:animated:, others have already made good answers. Basically you should use pushViewController:animated: when you replace your entires screen's content, and addSubview: when you add non-full screen UI elements to an existing view.

When we are talking about the view of a UIViewController, pushViewController:animated: should be your preferred method.

share|improve this answer
add comment

I've recently ran into similar problems with addSubview and pushViewController. Everyone here has made great comments, but I would add one things:

Usually addSubview is not used by itself. You usually are using it with presentModalViewController, or in the case of controller containment, addChildViewController.

So in summary:

  1. If you are using navigation controllers, you use pushViewController/popViewController to navigate through your app.
  2. If you are manually switching views, use presentModalViewController.
  3. If you are doing controller containment, use addChildViewController.
  4. If you are using story boards, use Segues.
share|improve this answer
add comment

Your Answer


