iOS-父子控制器
程序员文章站
2022-03-06 17:38:03
...
界面:
为什么需要使用父子控制器:
点击社会-头条-热点按钮:
1.相应的控制器每次都需要创建
2.相应的控制器的view每次都加上去
3.相应的控制器不能处理业务逻辑,控制器被销毁
注:
为什么view在,viewController不在(因为viewController被销毁所以不能处理业务逻辑)?
1.只添加了控制器的view并没有添加控制器,控制器没有强引用被销毁
解决方案:
1.在顶层ViewController设置强引用(强烈不推荐)
2.使用父子控制器(UIViewController管理子UIViewController)
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
//添加子控制器到父控制器
// 社会
ScoietyViewController *scoietyVc = [[ScoietyViewController alloc] init];
scoietyVc.title = @"社会";
[self addChildViewController:scoietyVc];
// 头条
TopLineViewController *topLineVc = [[TopLineViewController alloc] init];
topLineVc.title = @"头条";
[self addChildViewController:topLineVc];
// 热点
HotViewController *hotVc = [[HotViewController alloc] init];
hotVc.title = @"热点";
[self addChildViewController:hotVc];
//设置按钮内容
NSInteger count = self.titleContainView.subviews.count;
for (int i = 0; i < count; i++) {
UIButton *btn = self.titleContainView.subviews[i];
//获取当前控制器对象
UIViewController *vc = self.childViewControllers[i];
[btn setTitle:vc.title forState:UIControlStateNormal];
}
}
// 点击标题按钮
- (IBAction)showChildVcView:(UIButton *)sender {
// 移除之前控制器的view
[self.containView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
// for (UIView *vcView in self.containView.subviews) {
// [vcView removeFromSuperview];
// }
// 把对应子控制器的view添加上去
UIViewController *vc = self.childViewControllers[sender.tag];
vc.view.backgroundColor = sender.backgroundColor;
vc.view.frame = self.containView.bounds;
[self.containView addSubview:vc.view];
}
push
界面:
ViewController.m
@implementation ViewController
// UIViewController也是可以管理子控制器
- (void)viewDidLoad {
[super viewDidLoad];
// 添加一个子控制器
ChildViewController *childVc = [[ChildViewController alloc] init];
childVc.view.backgroundColor = [UIColor greenColor];
childVc.view.frame = CGRectMake(50, 50, 200, 200);
[self.view addSubview:childVc.view];
[self addChildViewController:childVc];
}
@end
ChildViewController.m
@implementation ChildViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"点击了子控制器的view %@",self.navigationController);
//点击了子控制器的view <UINavigationController: 0x7f8adc029a00>
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
/* 获取导航控制器
1.首先去判断下自己是否是导航控制器的子控制器,
2.否,判断父控制器是否是导航控制器的子控制器,
3.否,直到没有父控制器为止
*/
[self.navigationController pushViewController:vc animated:YES];
}
@end
modal
界面:
ViewController.m
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
ModalViewController *vc = [[ModalViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
[self presentViewController:vc animated:YES completion:nil];
}
ModalViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
ChildViewController *childVc = [[ChildViewController alloc] init];
childVc.view.backgroundColor = [UIColor blueColor];
childVc.view.frame = CGRectMake(50, 50, 200, 200);
[self.view addSubview:childVc.view];
[self addChildViewController:childVc];
}
ChildViewController.m
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"点击子控制器的view");
// 判断下当前方法调用者是否被modal,
//如果不是,判断父控制器是否被modal
[self dismissViewControllerAnimated:YES completion:nil];
}
上一篇: iOS 开发实训第十一周周报
下一篇: 6个方式教你如何提高网站权重