添加子视图控制器

//添加子视图控制器

- (void) setupChildViewControllers

{

NSArray * vcNames = @[@"PersonageFinancialViewController",@"EnterpriseFinancialViewController"];

for (NSInteger i = 0; i < vcNames.count; i ++) {

NSString * vcName = vcNames[i];

UIViewController * vc = [[NSClassFromString(vcName) alloc] init];

//当执行这句话,不会执行vc的viewDidLoad

[self addChildViewController:vc];

}

//进入子控制器加载第一个页面

[self scrollViewDidEndDecelerating:self.scrollView];

}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

CGFloat width = SCREEN_WIDTH;

CGFloat heitht = SCREEN_HEIGHT;

CGFloat offset = scrollView.contentOffset.x;

//获取索引值

NSInteger idx = scrollView.contentOffset.x / width;

//根据索引值返回vc引用

UIViewController * vc = self.childViewControllers[idx];

//判断当前vc是否执行过viewDidLoad

if ([vc isViewLoaded]) return;

//设置子控制器view的大小

vc.view.frame = CGRectMake(offset, 0, width, heitht);

//将子控制器的view加入scrollview上

[scrollView addSubview:vc.view];

}

你可能感兴趣的:(添加子视图控制器)