问题
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...
回答1:
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).
回答2:
-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.
回答3:
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.
回答4:
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.
回答5:
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:
- If you are using navigation controllers, you use p
ushViewController
/popViewController
to navigate through your app. - If you are manually switching views, use
presentModalViewController
. - If you are doing controller containment, use
addChildViewController
. - If you are using story boards, use Segues.
来源:https://stackoverflow.com/questions/11083551/whats-the-difference-of-pushviewcontroller-and-addsubview