Tan
Tan

Reputation: 179

what's the difference of pushViewController and addSubview

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

-(IBAction)DoneButtonPressed:(id)sender
{
    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...

Upvotes: 1

Views: 2501

Answers (5)

tng
tng

Reputation: 4346

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.

Upvotes: 0

Sumanth
Sumanth

Reputation: 4921

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.

Upvotes: 1

Paul Hunter
Paul Hunter

Reputation: 4463

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.

Upvotes: 0

nhahtdh
nhahtdh

Reputation: 56819

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).

Upvotes: 3

Jeffery Thomas
Jeffery Thomas

Reputation: 42598

-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.

Upvotes: 2

Related Questions