简体   繁体   中英

what's the difference between pushing a viewcontroller and adding the controller'view?

I want to show a view.What's the difference between these two situations?

UIViewController *con = [[dddd alloc]init];//this controller is used to show a pdf using a webView(using loadURL method)

//situation 1 which can show the pdf
[self.navigationcontroller pushviewcontroller:con animation:YES];

//situation 2 which can not show the pdf
[self.view addSubview:con.view];

In both situations,the [webView loadURL:url] is excuted,but the results are different,i don't know why.

The difference between these two approaches is that situation #2 :

  • The addSubview approach will not keep a strong reference to the new dddd view controller;

  • The addSubview approach also requires you to manually specify the frame for the new controller's view;

  • Even if you manually kept a strong reference to dddd , you will not receive key events in that view controller because your view controller hierarchy will not be consistent with your view hierarchy (see WWDC 2011 video Implementing UIViewController Containment , paid developer's subscription needed, for more information); and

  • You will not see any animation as you addSubview .

If you do the addSubview pattern, you should do the associated container calls (see Implementing a Container View Controller section in UIViewController Class Reference or the Creating Custom Container View Controllers of the View Controller Programming Guide ). At a bare minimum, you would:

UIViewController *con = [[dddd alloc]init];//this controller is used to show a pdf using a webView(using loadURL method)

[self addChildViewController:con];
[self.view addSubview:con.view];
[con didMoveToParentViewController:self];

And, when you want to remove it:

[con willMoveToParentViewController:nil];
[con.view removeFromSuperview];
[self removeChildViewController:con];

This will ensure that the view controller hierarchy is consistent with the view hierarchy, as well as ensure that the child view controller is retained. You'll have to manually perform any animation you want when you present this new view, though.

Generally it's easier to just do the appropriate pushViewController or presentViewController call, and this is not an issue. But in special cases, this custom container pattern can be useful.


By the way, if you want to show a PDF, consider using UIDocumentInteractionController , eg, specify that your view controller conforms to UIDocumentInteractionControllerDelegate and then use the following code:

- (IBAction)didTouchUpInsidePDFButton:(id)sender
{
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"sample" withExtension:@"pdf"];
    UIDocumentInteractionController *controller = [UIDocumentInteractionController interactionControllerWithURL:url];
    controller.delegate = self;
    [controller presentPreviewAnimated:YES];
}

- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller
{
    return self;

    // or if you want to push to the PDF preview, and the current view controller 
    // already has navigation controller, you can:
    //
    // return self.navigationController;
}

With UIDocumentInteractionController , you don't need to have your separate view controller with a UIWebView at all.

In short:
1) ViewController's transition has built-in animation support
2) ViewController's transition correctly handles view's lifecycle
3) ViewController usually handles all user interaction in it's view. So pushing of view controller is just good pattern to follow, say "Hey, system, now this ViewController is topmost, let him do the stuff", "And now this one", "And from now this one".

In terms of displaying views, with push and pop you can get almost same behaviour like with addSubview: removeFromSuperview .

Pushing a view controller open a new screen and enter in stack. Adding a view controller remain in same controller just a new window.

So pushing a view controller = new canvas adding a view controller = same canvas with new view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM