</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];
}