美文网首页
UIViewController的误用

UIViewController的误用

作者: 代码干货 | 来源:发表于2015-05-19 21:36 被阅读194次

    如果在一个UIViewController中有如下代码,你可能就在误用UIViewController
    <pre><code>
    viewController.view.bounds = CGRectMake(50, 50, 100, 200);
    [viewController.view addSubview:someOtherViewController.view];
    </code></pre>

    官方添加subView的方式

    Listing 14-1 Adding another view controller’s view to the container’s view hierarchy
    <pre><code>
    -(void) displayContentController: (UIViewController*) content;
    {
    [self addChildViewController:content]; // 1
    content.view.frame = [self frameForContentController]; // 2
    [self.view addSubview:self.currentClientView];
    [content didMoveToParentViewController:self]; // 3
    }
    </code></pre>

    Listing 14-2 Removing another view controller’s view to the container’s view hierarchy
    <pre><code>
    -(void) hideContentController: (UIViewController*) content
    {
    [content willMoveToParentViewController:nil]; // 1
    [content.view removeFromSuperview]; // 2
    [content removeFromParentViewController]; // 3
    }
    </code></pre>
    参考连接:

    UIViewController的误用
    0)Abusing UIViewController

    1. view controller programming guide for ios

    2)view controller basics

    相关文章

      网友评论

          本文标题:UIViewController的误用

          本文链接:https://www.haomeiwen.com/subject/abulqttx.html