父子控制器
- UITabBarController (parent)
- UINavigaionController (parent)
- childViewController ([child])
Example
push(点击子控制器push), modal(点击子控制器dismiss), 事件传递
view尽量交给controller保管 (想想tabbarcontroller)
思维过程:
按一个按钮时, 把对应控制器的view添加到当前控制器上
FirstViewController *vc = [[FBFirstViewController alloc]init];
vc.view.frame = CGRectMake(0, 64 + 55, self.view.bounds.size.width, self.view.bounds.size.height);
vc.view.backgroundColor = [UIColor greenColor];
[self.view addSubview:vc.view];
问题:
- 每次点击都要创建controller,
- 每次都要添加View
- 不能处理业务逻辑:因为controller是局部变量,没有强引用,所以被销毁,但它的view被添加到了数组里,不会被销毁
解决方案: 用强引用引用viewcontroller, 懒加载 可解决上面所有问题
[self.view addSubView:xxx] 被重复调用多次不会重复创建多个view
原理:先判断有没有父控件,有的话会先从superview中移除,再添加。
新问题: 子控制器无法得到navigationcontroller,无法push
解决: 父子控制器
新问题:子控制器点击dismiss:
解决: 父子控制器。因为调用dismiss会判断当前控制器是否是被modal出来的。如果不是detect它的父控制器是否被modal出来的
占位视图的思想。专门用一个view来显示子视图
用子视图不用设置frame了 [self.view addSubview:self.showingVc.view];`
反复调用添加同一个view不会添加多个。只会移到上面来。因为是同一个地址
[self.view addSubview:xxx];
- 被remove也不一定销毁,看有么有强指针引用
// add child viewController
- (void)viewDidLoad {
[superviewDidLoad];
self.allVces = @[
[[XMGOneViewController alloc] init],
[[XMGTwoViewController alloc] init],
[[XMGThreeViewController alloc] init]
];
}
- (IBAction)buttonClick:(UIButton*)button {
// 移除其他控制器的view
[self.showingVc.view removeFromSuperview];
// 获得控制器的位置(索引)
NSUIntegerindex = [button.superview.subviews indexOfObject:button];
// 添加控制器的view
self.showingVc =self.allVces[index];
self.showingVc.view.frame =CGRectMake(0,64,self.view.frame.size.width,self.view.frame.size.height -64);
[self.view addSubview:self.showingVc.view];
}
// 如果2个控制器的view是父子关系(不管是直接还是间接的父子关系),那么这2个控制器也应该为父子关系
[a.view addSubview:b.view];
[a addChildViewController:b];
// 或者
[a.view addSubview:otherView];
[otherView addSubbiew.b.view];
[a addChildViewController:b];
- (void)viewDidLoad {
[superviewDidLoad];
self.allVces = @[
[[XMGOneViewController alloc] init],
[[XMGTwoViewController alloc] init],
[[XMGThreeViewController alloc] init]
];
self.childViewControllers = @[
[[XMGOneViewController alloc] init],
[[XMGTwoViewController alloc] init],
[[XMGThreeViewController alloc] init]
];
// 通过addChildViewController添加的控制器都会存在于childViewControllers数组中
[selfaddChildViewController:[[XMGOneViewController alloc] init]];
[selfaddChildViewController:[[XMGTwoViewController alloc] init]];
[selfaddChildViewController:[[XMGThreeViewController alloc] init]];
// 将XMGOneViewController从childViewControllers数组中移除
[self.childViewControllers[0] removeFromParentViewController];
}
- (IBAction)buttonClick:(UIButton*)button {
// 移除其他控制器的view
[self.showingVc.view removeFromSuperview];
// 获得控制器的位置(索引)
NSUIntegerindex = [button.superview.subviews indexOfObject:button];
// 添加控制器的view
self.showingVc =self.childViewControllers[index];
self.showingVc.view.frame =CGRectMake(0,64,self.view.frame.size.width,self.view.frame.size.height -64);
[self.view addSubview:self.showingVc.view];
}
旋转屏幕,事件无法传递
/**
如果2个控制器的view是父子关系(不管是直接还是间接的父子关系),那么这2个控制器也应该为父子关系
[a.view addSubview:b.view];
[a addChildViewController:b];
或者
[a.view addSubview:otherView];
[otherView addSubbiew.b.view];
[a addChildViewController:b];
*/
(void)viewDidLoad {
[superviewDidLoad];
// self.allVces = @[
// [[XMGOneViewController alloc] init],
// [[XMGTwoViewController alloc] init],
// [[XMGThreeViewController alloc] init]
// ];
// self.childViewControllers = @[
// [[XMGOneViewController alloc] init],
// [[XMGTwoViewController alloc] init],
// [[XMGThreeViewController alloc] init]
// ];
// 通过addChildViewController添加的控制器都会存在于childViewControllers数组中
[selfaddChildViewController:[[XMGOneViewController alloc] init]];
[selfaddChildViewController:[[XMGTwoViewController alloc] init]];
[selfaddChildViewController:[[XMGThreeViewController alloc] init]];
// 将XMGOneViewController从childViewControllers数组中移除
// [self.childViewControllers[0] removeFromParentViewController];
}
- (IBAction)buttonClick:(UIButton*)button {
// 移除其他控制器的view
[self.showingVc.view removeFromSuperview];
// 获得控制器的位置(索引)
NSUIntegerindex = [button.superview.subviews indexOfObject:button];
// 添加控制器的view
self.showingVc =self.childViewControllers[index];
self.showingVc.view.frame =CGRectMake(0,64,self.view.frame.size.width,self.view.frame.size.height -64);
[self.view addSubview:self.showingVc.view];
}