oc addChildViewController

实现类似于新闻端多个标签页。
此处转载于 http://www.tuicool.com/articles/3ymMzub
@interface HMTMainViewController () <UIScrollViewDelegate>

@property (nonatomic ,strong) HMTThirdViewController  *thirdVC;
@property (nonatomic ,strong) HMTFirstViewController  *firstVC;
@property (nonatomic ,strong) HMTSecondViewController *secondVC;

@property (nonatomic ,strong) UIViewController *currentVC;

@property (nonatomic ,strong) UIScrollView *headScrollView;  //  顶部滚动视图

@property (nonatomic ,strong) NSArray *headArray;

@end

@implementation HMTMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  if (self) {
    // Custom initialization
  }
  return self;
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view.
   
  self.navigationItem.title = @"网易新闻Demo";
  
  self.headArray = @[@"头条",@"娱乐",@"体育",@"财经",@"科技",@"NBA",@"手机"];
  /**
   *   automaticallyAdjustsScrollViewInsets   又被这个属性坑了
   *   我"UI高级"里面一篇文章着重讲了它,大家可以去看看
   */
  self.automaticallyAdjustsScrollViewInsets = NO;
  self.headScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, 320, 40)];
  self.headScrollView.backgroundColor = [UIColor purpleColor];
  self.headScrollView.contentSize = CGSizeMake(560, 0);
  self.headScrollView.bounces = NO;
  self.headScrollView.pagingEnabled = YES;
  [self.view addSubview:self.headScrollView];
  for (int i = 0; i < [self.headArray count]; i++) {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0 + i*80, 0, 80, 40);
    [button setTitle:[self.headArray objectAtIndex:i] forState:UIControlStateNormal];
    button.tag = i + 100;
    [button addTarget:self action:@selector(didClickHeadButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headScrollView addSubview:button];
    
  }
  
  /*
   苹果新的API增加了addChildViewController方法,并且希望我们在使用addSubview时,同时调用[self addChildViewController:child]方法将sub view对应的viewController也加到当前ViewController的管理中。
   对于那些当前暂时不需要显示的subview,只通过addChildViewController把subViewController加进去;需要显示时再调用transitionFromViewController方法。将其添加进入底层的ViewController中。
   这样做的好处:
   
   1.无疑,对页面中的逻辑更加分明了。相应的View对应相应的ViewController。
   2.当某个子View没有显示时,将不会被Load,减少了内存的使用。
   3.当内存紧张时,没有Load的View将被首先释放,优化了程序的内存释放机制。
   */
  
  /**
   *  在iOS5中,ViewController中新添加了下面几个方法:
   *  addChildViewController:
   *  removeFromParentViewController
   *  transitionFromViewController:toViewController:duration:options:animations:completion:
   *  willMoveToParentViewController:
   *  didMoveToParentViewController:
   */
  self.firstVC = [[HMTFirstViewController alloc] init];
  [self.firstVC.view setFrame:CGRectMake(0, 104, 320, 464)];
  [self addChildViewController:_firstVC];
  
  self.secondVC = [[HMTSecondViewController alloc] init];
  [self.secondVC.view setFrame:CGRectMake(0, 104, 320, 464)];
  //这段代码会触发viewdidload
  self.thirdVC = [[HMTThirdViewController alloc] init];
  [self.thirdVC.view setFrame:CGRectMake(0, 104, 320, 464)];
  
  //  默认,第一个视图(你会发现,全程就这一个用了addSubview)
  [self.view addSubview:self.firstVC.view];
  self.currentVC = self.firstVC;
  //addSubView后相应的VC viewWillAppear将会调用。前提是VC已经加入本VC。

}

- (void)didClickHeadButtonAction:(UIButton *)button
{
  //  点击处于当前页面的按钮,直接跳出
  if ((self.currentVC == self.firstVC && button.tag == 100)||(self.currentVC == self.secondVC && button.tag == 101.)) {
    return;
  }else{
  
    //  展示2个,其余一样,自行补全噢
    switch (button.tag) {
      case 100:
        [self replaceController:self.currentVC newController:self.firstVC];
        break;
      case 101:
        [self replaceController:self.currentVC newController:self.secondVC];
        break;
      case 102:
        //.......
        break;
      case 103:
        //.......
        break;
      case 104:
        //.......
        break;
      case 105:
        //.......
        break;
      case 106:
        //.......
        break;
        //.......
      default:
        break;
    }
  }

}

//  切换各个标签内容
- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
{
  /**
   *			着重介绍一下它
   *  transitionFromViewController:toViewController:duration:options:animations:completion:
   *  fromViewController	  当前显示在父视图控制器中的子视图控制器
   *  toViewController		将要显示的姿势图控制器
   *  duration				动画时间(这个属性,old friend 了 O(∩_∩)O)
   *  options				 动画效果(渐变,从下往上等等,具体查看API)
   *  animations			  转换过程中得动画
   *  completion			  转换完成
   */
  
  [self addChildViewController:newController];
  [self transitionFromViewController:oldController toViewController:newController duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
  //调用上边方法后,相应VC的viewWillDisapear和viewWillApear将会调用。      
    if (finished) {
          
      [newController didMoveToParentViewController:self];
      [oldController willMoveToParentViewController:nil];
      [oldController removeFromParentViewController];
      self.currentVC = newController;
        
    }else{
          
      self.currentVC = oldController;
        
    }
  }];
}
上边的能满足基本点击需求,但是要支持左右滑动切换标签就要用scrollView包一下子VC,如果标签过多就要有相应的策略优化了,比如复用、设置维护可展示队列等。
觉得这类的展现优化可以分为两种优化方式:
1.多个标签要展示且有可复用的展现类型,需要展现类型不多的话用collectionview管理,因为其自身维护了一套复用机制。
2. 多个标签要展示且类型比较多或是没有可复用的情况,就要有相应策略 维护可展示队列,不要全缓存消耗内存。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OC中添加子ViewController也可以使用类似的步骤,具体如下: 1.创建子ViewController 首先,需要创建一个子ViewController,可以使用以下代码: ``` ChildViewController *childVC = [[ChildViewController alloc] init]; ``` 其中ChildViewController是你要添加的子ViewController类。 2.将子ViewController添加为子视图控制器 同样,将子ViewController添加为父ViewController的子视图控制器,可以使用以下代码: ``` [self addChildViewController:childVC]; ``` 3.将子ViewController的视图添加到父ViewController中 现在,可以将子ViewController的视图添加到父ViewController中,可以使用以下代码: ``` [self.view addSubview:childVC.view]; ``` 这里,我们将子ViewController的视图添加到父ViewController的view中。 4.完成添加 最后,需要调用以下代码来完成添加: ``` [childVC didMoveToParentViewController:self]; ``` 这里,我们调用了childVC的didMoveToParentViewController:方法,通知子ViewController已经添加到了父ViewController中。 完整的代码如下所示: ``` ChildViewController *childVC = [[ChildViewController alloc] init]; [self addChildViewController:childVC]; [self.view addSubview:childVC.view]; [childVC didMoveToParentViewController:self]; ``` 需要注意的是,在移除子ViewController时,需要按照相反的顺序执行上述步骤,并在最后调用以下方法来从父视图控制器中删除子视图控制器: ``` [childVC removeFromParentViewController]; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值