您的位置:首页 > 其它

ParentViewController中添加多个SubViewController的View

2015-03-10 14:22 316 查看
在storyboard中创建一个ParentViewController,创建多个SubViewController。

我在子控制器上添加的是tableView,然后在父控制器中普通的添加子控制器的View,发现tableView的代理已不执行,这很可能由于复杂的添加使tableView已经释放。

1.在父控制器创建两个子控制器的变量;

@property (nonatomic, strong) HouseholdsViewController *householdsViewController;
@property (nonatomic, strong) BusinessViewController *businessViewController;

2.重写两个子控制器的getter方法,storyboard的标识是在storyboard里关联自控制设置的标识;
- (HouseholdsViewController *)householdsViewController{
if(!_householdsViewController){
_householdsViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"HouseholdsViewController"];
}
return _householdsViewController;
}
- (BusinessViewController *)businessViewController{
if(!_businessViewController){
_businessViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"BusinessViewController"];
}
return _businessViewController;
}
3.点击添加自控制器View的方法

- (IBAction)SegmentedControlValueChanged:(UISegmentedControl *)sender {

switch (sender.selectedSegmentIndex) {
case 0:
{
[self addChildViewController:self.householdsViewController];
self.householdsViewController.view.frame = CGRectMake(0, 64, kMainScreenWidth, kMainScreenHeight-64);
[self.view addSubview:self.householdsViewController.view];
[self.householdsViewController didMoveToParentViewController:self];

}
break;
case 1:
{
[self addChildViewController:self.businessViewController];
self.businessViewController.view.frame = CGRectMake(0, 64, kMainScreenWidth, kMainScreenHeight-64);
[self.view addSubview:self.businessViewController.view];
[self.businessViewController didMoveToParentViewController:self];
}
break;

default:
break;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐