1.0多控制器切换-灵活的TabBarController 随意切换 实现复杂应用 不合理(因为没有实现父子控制器 父控制器无法传递如旋转等事件给自控制器)
--------------------------------
模拟苹果官方做法 TabBarController控制器做法 NavigationController做法也一样 都是push做法(push 就是add一个控制器的view remove其他控制器的view )
点击按钮 分别展示不同控制器的view 当前控制器强引用多个控制器(懒加载) 点击不同button展示不同的控制器
view1和vc1共存亡 , 因为view1身上的事件还需要控制器vc1去管理 才能保证view1内部的数据和业务逻辑正常
当前控制器强引用 多个控制器 @property(nonatomic,strong) UIViewController *vc1; @property(nonatomic,strong) UIViewController *vc2; ....
- (IBAction)btn3Click { //点击btn3 其他控制器的view从当前控制器 self.view.subviews数组里移除 这时候每个view都有一个控制器强引用其view [self.twovc.view removeFromSuperview]; [self.onevc.view removeFromSuperview]; [self.view addSubview:self.threevc.view]; }
- (HMThreeViewController *)threevc { if (!_three) { self.threevc = [[HMThreeViewController alloc] init]; self.threevc.view.frame = CGRectMake(10, 70, 300, 300); } return _three; }
具体代码:
// // HMViewController.m #import "HMViewController.h" #import "HMTestViewController.h" #import "HMOneViewController.h" #import "HMTwoViewController.h" #import "HMThreeViewController.h" @interface HMViewController () - (IBAction)vc1; - (IBAction)vc2; - (IBAction)vc3; @property (nonatomic, strong) HMTestViewController *test; @property (nonatomic, strong) HMOneViewController *one; @property (nonatomic, strong) HMTwoViewController *two; @property (nonatomic, strong) HMThreeViewController *three; @end @implementation HMViewController - (HMOneViewController *)one { if (!_one) { self.one = [[HMOneViewController alloc] init]; self.one.view.frame = CGRectMake(10, 70, 300, 300); } return _one; } - (HMTwoViewController *)two { if (!_two) { self.two = [[HMTwoViewController alloc] init]; self.two.view.frame = CGRectMake(10, 70, 300, 300); } return _two; } - (HMThreeViewController *)three { if (!_three) { self.three = [[HMThreeViewController alloc] init]; self.three.view.frame = CGRectMake(10, 70, 300, 300); } return _three; } - (void)viewDidLoad { [super viewDidLoad]; // HMTestViewController *test = [[HMTestViewController alloc] init]; // test.view.frame = CGRectMake(100, 100, 200, 300); // test.view.backgroundColor = [UIColor redColor]; // [self.view addSubview:test.view]; // self.test = test; // 如果发现:控制器的view还在,但是view上面的数据不显示,极大可能是因为:控制器被提前销毁了 // 1.一个控制器的view是可以随意调整尺寸和位置的 // 2.一个控制器的view是可以随意添加到其他view中 // 3.如果将一个控制器的view,添加到其他view中显示,那么要想办法保证控制器不被销毁 // 4.原则:只要view在,view所在的控制器必须得在,这样才能保证view内部的数据和业务逻辑正常 } - (IBAction)vc1 { [self.two.view removeFromSuperview]; [self.three.view removeFromSuperview]; [self.view addSubview:self.one.view]; } - (IBAction)vc2 { [self.one.view removeFromSuperview]; [self.three.view removeFromSuperview]; [self.view addSubview:self.two.view]; } - (IBAction)vc3 { [self.two.view removeFromSuperview]; [self.one.view removeFromSuperview]; [self.view addSubview:self.three.view]; } @end
-----------------------------------
vc1->view1
[self.view addSubView:view1] 数组里有一个强引用地址指向这个对象 [self.vc1.view1 removeFromSuperView] 就是将这个数组的一个强引用地址取消一个
但是这个view1还是属于自控制器vc1在管理的 所以没有被销毁
[self.view addSubView:view1] view上add多次同一个view1 那么只有一次是生效的
[self.view addSubView:view1]
[self.view addSubView:view1]
----------------
抽屉效果:左边view1 点击就出来了 是这样做出来的:先把view1的x设置-100 点击一下然后将其x变成0
---------------

浙公网安备 33010602011771号