ios - 推送 View Controller 和添加 Controller View 有什么区别?

标签 ios navigation viewcontroller addsubview

我想展示一个 View 。这两种情况有什么区别?

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];

在这两种情况下,[webView loadURL:url] 都被执行,但结果不同,我不知道为什么。

最佳答案

这两种方法之间的区别在于情况 #2:

  • addSubview方法不会保留对新的 dddd 的强烈引用 View Controller ;
  • addSubview方法还需要您手动指定 frame对于新 Controller 的 View ;
  • 即使您手动保留了对 dddd 的强引用,您将不会在该 View Controller 中收到关键事件,因为您的 View Controller 层次结构与您的 View 层次结构不一致(请参阅 WWDC 2011 视频 Implementing UIViewController Containment,需要付费开发者订阅,了解更多信息);和
  • 您将看不到任何动画 addSubview .

  • 如果您执行 addSubview模式,您应该执行相关的容器调用(参见 UIViewController 类引用中的 Implementing a Container View Controller 部分或 View Controller 编程指南的 Creating Custom Container View Controllers)。至少,你会:
    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];
    

    而且,当你想删除它时:
    [con willMoveToParentViewController:nil];
    [con.view removeFromSuperview];
    [self removeChildViewController:con];
    

    这将确保 View Controller 层次结构与 View 层次结构保持一致,并确保保留 subview Controller 。但是,当您展示这个新 View 时,您必须手动执行您想要的任何动画。

    通常只做适当的 pushViewController 会更容易。或 presentViewController打电话,这不是问题。但在特殊情况下,这种自定义容器模式可能很有用。

    顺便说一句,如果您想显示 PDF,请考虑使用 UIDocumentInteractionController ,例如,指定您的 View Controller 符合 UIDocumentInteractionControllerDelegate然后使用以下代码:
    - (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;
    }
    

    UIDocumentInteractionController ,您不需要使用带有 UIWebView 的单独 View Controller 一点也不。

    关于ios - 推送 View Controller 和添加 Controller View 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20031611/

    相关文章:

    ios - 如何从字符串中解析十进制而不丢失精度?

    ios - 如何使用 Apple Localization Glossaries/AppleGlot 翻译 iPhone 应用程序中的基本术语?

    ios - 如何制作具有默认文本的 iOS 8 共享扩展

    ios - UIPopoverController 奇怪的调整大小行为

    ios - 将 Subview 传递给子类 UIView

    Xamarin 窗体 : How to change the back button color

    android - 在 React Native 中制作 slider 导航的最佳方法

    jquery 向右展开 <li> 向左折叠

    ios - 两个 ViewController - 第一个有 x 个按钮,第二个有 x 个标签

    ios - 在您离开并返回 View Controller 后将 View Controller 保持在它的状态 - Swift