iOS -- childViewController 的问题

来源:互联网 发布:talkingdata数据造假 编辑:程序博客网 时间:2024/06/07 03:43

这里写图片描述
做开发,会经常碰到图片这种类似的需求.或是segmentControl 或者两个button控制两个界面直接的切换
推荐一个比较好用的方法
新建两个viewController,我们命名为FirstViewController 和 SecondViewController
在RootViewController的代码如下

#import "RootViewController.h"#import "FirstViewController.h"#import "SecondTableViewController.h"@interface RootViewController ()<UIScrollViewDelegate>@property (nonatomic, strong) UISegmentedControl *segmentedControl;@property (nonatomic, strong) UIScrollView *scrollView;@property (nonatomic, strong) FirstViewController *firstVC;@property (nonatomic, strong) SecondTableViewController *secondTVC;@end@implementation RootViewController- (void)viewDidLoad{    [super viewDidLoad];    // 适应scrollView    self.automaticallyAdjustsScrollViewInsets = NO;    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"first", @"second"]];    self.navigationItem.titleView = self.segmentedControl;    [self.segmentedControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];    self.segmentedControl.selectedSegmentIndex = 0;    // 创建scrollView    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64)];    [self.view addSubview:self.scrollView];    // 设置scrollView的内容    self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 2, [UIScreen mainScreen].bounds.size.height - 64);    self.scrollView.pagingEnabled = YES;    self.scrollView.bounces = NO;    // 创建控制器    self.firstVC = [FirstViewController new];    self.secondTVC = [[SecondTableViewController alloc] initWithStyle:UITableViewStylePlain];    // 添加为self的子控制器    [self addChildViewController:self.firstVC];    [self addChildViewController:self.secondTVC];    self.firstVC.view.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));    self.secondTVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));    [self.scrollView addSubview:self.firstVC.view];    [self.scrollView addSubview:self.secondTVC.view];    // 设置scrollView的代理    self.scrollView.delegate = self;}

这里的要点就是我们在RootViewController上添加两个子视图FirstViewController 和 SecondViewController

// 添加为self的子控制器    [self addChildViewController:self.firstVC];    [self addChildViewController:self.secondTVC];

这样写的好处是将两个页面完全与RootViewController完全独立开来,各自处理各自的数据与页面内容.
碰到类似的情况了,都可以用viewController作为childViewController去添加.比起添加自定义的UIView方便一点
比如页面跳转,我们点击firstViewController的时候,就可以用

[self.parentViewController.navigationController pushViewController:<#(nonnull UIViewController *)#> animated:YES];

直接调用父视图的navigationController跳转.

/** * 分段控制器点击方法 */- (void)segmentedControlAction:(UISegmentedControl *)sender{    [self.scrollView setContentOffset:CGPointMake(sender.selectedSegmentIndex * self.scrollView.frame.size.width, 0) animated:NO];}- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    NSInteger n = scrollView.contentOffset.x / scrollView.frame.size.width;    self.segmentedControl.selectedSegmentIndex = n;}
0 0
原创粉丝点击