iOS addChildViewController 详解

      突然发现公司小弟对 addChildViewController这个方法居然不怎么了解,找了篇帖子 记录下。

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  YYMainViewController.m  
  3. //  UIScrollView  

  4. #import "YYMainViewController.h"  
  5. #import "YYFirstViewController.h"  
  6. #import "YYSecondViewController.h"  
  7. #import "YYThirdViewController.h"  
  8.   
  9. @interface YYMainViewController () <UIScrollViewDelegate>  
  10.   
  11. @property (nonatomic ,strong) YYThirdViewController  *thirdVC;  
  12. @property (nonatomic ,strong) YYFirstViewController  *firstVC;  
  13. @property (nonatomic ,strongYYSecondViewController *secondVC;  
  14.   
  15. @property (nonatomic ,strongUIViewController *currentVC;  
  16.   
  17. @property (nonatomic ,strongUIScrollView *headScrollView;  //  顶部滚动视图  
  18.   
  19. @property (nonatomic ,strongNSArray *headArray;  
  20.   
  21. @end  
  22.   
  23. @implementation YYMainViewController  
  24.   
  25. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  26. {  
  27.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  28.     if (self) {  
  29.         // Custom initialization  
  30.     }  
  31.     return self;  
  32. }  
  33.   
  34. - (void)viewDidLoad  
  35. {  
  36.     [super viewDidLoad];  
  37.     // Do any additional setup after loading the view.  
  38.      
  39.     self.navigationItem.title = @"网易新闻Demo";  
  40.       
  41.     self.headArray = @[@"头条",@"娱乐",@"体育",@"财经",@"科技",@"NBA",@"手机"];  
  42.     /** 
  43.      *   automaticallyAdjustsScrollViewInsets   又被这个属性坑了 
  44.      *   我"UI高级"里面一篇文章着重讲了它,大家可以去看看 
  45.      */  
  46.     self.automaticallyAdjustsScrollViewInsets = NO;  
  47.     self.headScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(06432040)];  
  48.     self.headScrollView.backgroundColor = [UIColor purpleColor];  
  49.     self.headScrollView.contentSize = CGSizeMake(5600);  
  50.     self.headScrollView.bounces = NO;  
  51.     self.headScrollView.pagingEnabled = YES;  
  52.     [self.view addSubview:self.headScrollView];  
  53.     for (int i = 0; i < [self.headArray count]; i++) {  
  54.           
  55.         UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];  
  56.         button.frame = CGRectMake(0 + i*8008040);  
  57.         [button setTitle:[self.headArray objectAtIndex:i] forState:UIControlStateNormal];  
  58.         button.tag = i + 100;  
  59.         [button addTarget:self action:@selector(didClickHeadButtonAction:) forControlEvents:UIControlEventTouchUpInside];  
  60.         [self.headScrollView addSubview:button];  
  61.           
  62.     }  
  63.       
  64.     /* 
  65.      苹果新的API增加了addChildViewController方法,并且希望我们在使用addSubview时,同时调用[self addChildViewController:child]方法将sub view对应的viewController也加到当前ViewController的管理中。 
  66.      对于那些当前暂时不需要显示的subview,只通过addChildViewController把subViewController加进去;需要显示时再调用transitionFromViewController方法。将其添加进入底层的ViewController中。 
  67.      这样做的好处: 
  68.       
  69.      1.无疑,对页面中的逻辑更加分明了。相应的View对应相应的ViewController。 
  70.      2.当某个子View没有显示时,将不会被Load,减少了内存的使用。 
  71.      3.当内存紧张时,没有Load的View将被首先释放,优化了程序的内存释放机制。 
  72.      */  
  73.       
  74.     /** 
  75.      *  在iOS5中,ViewController中新添加了下面几个方法: 
  76.      *  addChildViewController: 
  77.      *  removeFromParentViewController 
  78.      *  transitionFromViewController:toViewController:duration:options:animations:completion: 
  79.      *  willMoveToParentViewController: 
  80.      *  didMoveToParentViewController: 
  81.      */  
  82.     self.firstVC = [[YYFirstViewController alloc] init];  
  83.     [self.firstVC.view setFrame:CGRectMake(0104320464)];  
  84.     [self addChildViewController:_firstVC];  
  85.       
  86.     self.secondVC = [[YYSecondViewController alloc] init];  
  87.     [self.secondVC.view setFrame:CGRectMake(0104320464)];  
  88.       
  89.     self.thirdVC = [[YYThirdViewController alloc] init];  
  90.     [self.thirdVC.view setFrame:CGRectMake(0104320464)];  
  91.       
  92.     //  默认,第一个视图(你会发现,全程就这一个用了addSubview)  
  93.     [self.view addSubview:self.firstVC.view];  
  94.     self.currentVC = self.firstVC;  
  95.       
  96. }  
  97.   
  98. - (void)didClickHeadButtonAction:(UIButton *)button  
  99. {  
  100.     //  点击处于当前页面的按钮,直接跳出  
  101.     if ((self.currentVC == self.firstVC && button.tag == 100)||(self.currentVC == self.secondVC && button.tag == 101.)) {  
  102.         return;  
  103.     }else{  
  104.       
  105.         //  展示2个,其余一样,自行补全噢  
  106.         switch (button.tag) {  
  107.             case 100:  
  108.                 [self replaceController:self.currentVC newController:self.firstVC];  
  109.                 break;  
  110.             case 101:  
  111.                 [self replaceController:self.currentVC newController:self.secondVC];  
  112.                 break;  
  113.             case 102:  
  114.                 //.......  
  115.                 break;  
  116.             case 103:  
  117.                 //.......  
  118.                 break;  
  119.             case 104:  
  120.                 //.......  
  121.                 break;  
  122.             case 105:  
  123.                 //.......  
  124.                 break;  
  125.             case 106:  
  126.                 //.......  
  127.                 break;  
  128.                 //.......  
  129.             default:  
  130.                 break;  
  131.         }  
  132.     }  
  133.   
  134. }  
  135.   
  136. //  切换各个标签内容  
  137. - (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController  
  138. {  
  139.     /** 
  140.      *            着重介绍一下它 
  141.      *  transitionFromViewController:toViewController:duration:options:animations:completion: 
  142.      *  fromViewController      当前显示在父视图控制器中的子视图控制器 
  143.      *  toViewController        将要显示的姿势图控制器 
  144.      *  duration                动画时间(这个属性,old friend 了 O(∩_∩)O) 
  145.      *  options                 动画效果(渐变,从下往上等等,具体查看API) 
  146.      *  animations              转换过程中得动画 
  147.      *  completion              转换完成 
  148.      */  
  149.       
  150.     [self addChildViewController:newController];  
  151.     [self transitionFromViewController:oldController toViewController:newController duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {  
  152.                   
  153.         if (finished) {  
  154.                       
  155.             [newController didMoveToParentViewController:self];  
  156.             [oldController willMoveToParentViewController:nil];  
  157.             [oldController removeFromParentViewController];  
  158.             self.currentVC = newController;  
  159.                   
  160.         }else{  
  161.                       
  162.             self.currentVC = oldController;  
  163.                   
  164.         }  
  165.     }];  
  166. }  

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值