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;
}
}
我在子控制器上添加的是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;
}
}
相关文章推荐
- ParentViewController中添加SubViewController(IOS学习)
- [addChildViewController]View Controller中可以添加多个sub view,在需要的时候显示出来;
- ios UIImagePickerController 添加一个自定义的view
- self.parentViewController dismissModalViewController在iOS不起作用
- IOS UISplitViewController之 解决 密码框添加之后 调用子视图的转向方法之时 对象为空
- iOS 5.0 后UIViewController新增:willMoveToParentViewController和didMoveToParentViewCon
- 使用XIB自定义一个UIView,然后将这个view添加到controller的view
- willMoveToParentViewController和didMoveToParentViewController
- iOS 5.0 后UIViewController新增:willMoveToParentViewController和didMoveToParentViewCon
- iOS 导航条上添加UISegmentedControl,点击分类按钮,显示不同的ViewController
- willMoveToParentViewController和didMoveToParentViewController
- 添加到父类视图后,subview的高度莫名发生变化
- iOS viewController添加导航条以及返回跳转选择
- iOS 5.0 后UIViewController新增:willMoveToParentViewController和didMoveToParentViewCon
- removeFromParentViewController
- willMoveToParentViewController和didMoveToParentViewController
- 使用XIB自定义一个UIView,然后将这个view添加到controller的view
- UIViewController 中的 willMoveToParentViewController和didMoveToParentViewController的使用
- 使用XIB自定义一个UIView,然后将这个view添加到controller的view 上(相当于所有界面都通过xib来实现)
- iOS -- UIViewController 中的 willMoveToParentViewController和didMoveToParentViewController的使用