</pre>视图控制器:ViewController<p></p><p><span style="font-size:18px"></span></p><p><span style="font-size:18px">在iOS程序开发中官方提供了四种切换ViewController的方法:</span></p><p><span style="font-size:18px"></span></p><p><span style="font-size:18px">01 模态视图切换</span></p><p><span style="font-size:18px">02 <span style="font-family:Menlo">UINavigationController</span></span></p><p><span style="font-size:18px"><span style="font-family:Menlo">03 </span><span style="font-family:Menlo">UITabBarController</span></span></p><p><span style="font-size:18px"><span style="font-family:Menlo">04 </span><span style="color:rgb(52,149,175); font-family:Menlo">addChildViewController</span></span></p><p><span style="font-size:18px"><span style="color:rgb(52,149,175); font-family:Menlo"> 在子视图控制中如何获取容器视图控制器呢?</span></span></p><p><span style="font-size:18px"><span style="color:rgb(52,149,175); font-family:Menlo"><span style="white-space:pre"> </span></span></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: 'Heiti SC Light'; color: rgb(0, 143, 0);"><span style="font-family: 'Helvetica Neue';"><span style="white-space:pre"> </span>//</span>找到当前视图控制器所在的容器</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(52, 149, 175);"><span style="color: #000000"> </span>PSBMedicalViewController<span style="color: #000000"> * mvc = (</span><span style="font-family: 'Helvetica Neue'; color: rgb(4, 51, 255);">id</span><span style="color: #000000">)</span><span style="font-family: 'Helvetica Neue'; color: rgb(4, 51, 255);">self</span><span style="color: #000000">.</span>parentViewController<span style="color: #000000">;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; min-height: 21px;"> </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(52, 149, 175);"><span style="color: #000000"> [mvc </span>popRightDrawer<span style="color: #000000">];</span></p><p><span style="font-size:18px; color:#cc0000"><span style="font-family:Menlo">第四种方式我们用来自定义容器时,切换ViewController。</span></span></p><p><span style="font-size:18px"><span style="color:rgb(52,149,175); font-family:Menlo"></span></span></p><p><span style="font-size:18px"><span style="font-family:Menlo">视图控制器有两类:1:显示的视图控制器 2:不显示的视图控制器,用来管理其他子视图控制器的视图控制器。称为:容器视图控制器。</span></span></p><p></p><p><span style="white-space:pre"> </span><span style="font-size:24px;color:#cc0000;">注意:自定义的容器视图控制器,是不能被释放的。为此,有多种方式让其不被释放,1:单例,2:交给NavigationBar中ViewControllers这个数组。</span></p><p></p><p></p><pre name="code" class="objc">#import "PSBRootViewController.h" /**自定义的容器控制器,管理其他的视图控制器,本身不显示*/ @interface AZMedicalViewController : PSBRootViewController //弹出右侧的抽屉 -(void)popRightDrawer; //收起右侧抽屉 -(void)hideRightDrawer; @end
#import "AZMedicalViewController.h" #import "AZFirstLayerModel.h" @interface AZMedicalViewController () { //判断当前抽屉是否显示 BOOL _isDrawerShow; } @end @implementation AZMedicalViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; //创建相关子视图 [self createSubView]; //在这里,添加被管理的子视图控制器 [self createChildViewcontrollers]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //创建相关子视图 -(void)createSubView { PSBBarButtonItem *backButton=[PSBViewFactory backButton]; [backButton addTarget:self action:@selector(backButtonOnClick:) forControlEvents:UIControlEventTouchUpInside]; [_nvgItem addSubview:backButton]; } -(void)backButtonOnClick:(PSBBarButtonItem *)item { if (!_isDrawerShow) { [self.navigationController popViewControllerAnimated:YES]; } else { [self hideRightDrawer]; } } #pragma mark -- 容器视图控制器的相关方法 //添加子视图控制器 -(void)addChildViewControllers:(NSArray *)vcs { if (vcs.count!=2) { return; } for (NSUInteger i=0; i<2; i++) { PSBRootViewController *vc=vcs[i]; //让出导航条 CGRect frame=self.view.bounds; frame.origin.y += 64; frame.size.height -=64; if (i==0) { vc.view.frame=frame; } else { frame.size.width=frame.size.width/2; frame.origin.x=self.view.bounds.size.width; vc.view.frame=frame; } [self.view addSubview:vc.view]; //但是需要注意:如果在ARC下,self.view 只添加了vc.view的话,那么arc会自动释放掉VC。 //解决办法: //官方视图控制器,提供方法保留子视图控制器。 //使当前视图控制器作为容器,子视图控制器不会被释放。其实内部实现也是很简单,就是一个数组,管理着这些子视图控制器,强引用。 [self addChildViewController:vc]; } } //弹出右侧的抽屉 -(void)popRightDrawer { if (_isDrawerShow) { return; } [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.25]; //设置动画结束的效果 PSBRootViewController *vc1=self.childViewControllers[0]; CGRect frame=vc1.view.frame; frame.size.width=frame.size.width/2; vc1.view.frame=frame; PSBRootViewController *vc2=self.childViewControllers[1]; frame=vc2.view.frame; frame.origin.x=vc1.view.frame.size.width; vc2.view.frame=frame; [UIView commitAnimations]; _isDrawerShow=YES; } //收起右侧抽屉 -(void)hideRightDrawer { if (!_isDrawerShow) { return; } [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.25]; //设置动画的效果 CGRect frame=self.view.bounds; frame.origin.y +=64; frame.size.height -=64; PSBRootViewController *vc1=self.childViewControllers[0]; vc1.view.frame=frame; PSBRootViewController *vc2=self.childViewControllers[1]; frame.origin.x=vc1.view.frame.size.width; frame.size.width=self.view.bounds.size.width/2; vc2.view.frame=frame; [UIView commitAnimations]; _isDrawerShow=NO; } #pragma mark -- 子视图控制器 //创建子视图控制器 -(void)createChildViewcontrollers { NSArray *tempArray=@[@"AZFirstViewController",@"AZSecondViewController"]; id obj1,obj2; for (NSUInteger i=0; i<tempArray.count; i++) { Class cls=NSClassFromString(tempArray[i]); id obj=[[cls alloc] init]; if (i) { obj2=obj; } else { obj1=obj; } } //添加到当前容器 [self addChildViewControllers:@[obj1,obj2]]; } @end
//选中了一个cell - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //找到当前视图控制器所在的容器 PSBMedicalViewController * mvc = (id)self.parentViewController; [mvc popRightDrawer]; }