在項目開發中使用iOS自帶的導航,因為其樣式修改起來比較麻煩,這裡我自己寫了一個類似的切換方式。我自己覺得還挺好用的,可能是因為代碼是自己寫的吧。。
先把代碼附上:
BaseContentViewController.h
// // BaseContentViewController.h // NewPICCProject // // Created by 杜甲 on 14-1-26. // Copyright (c) 2014年 杜甲. All rights reserved. // #importtypedef enum enViewControllersId { HomeVC_Id = 0, //首頁 ViewController_Id,//主視圖 ProvincialCityVC_Id,//數據對比 ComparisonVC_Id //顯示對比 }EnViewControllersId; @interface BaseContentViewController : UIViewController @property (strong , nonatomic) NSMutableDictionary* m_DicControllers; //存放視圖控制器的字典 @property (assign , nonatomic) NSInteger m_nCurrentViewControllerID; //記錄當前顯示視圖的ID //界面切換的方法 -(void)changeViewControllerToCenter:(EnViewControllersId) aViewControllersId; //存儲視圖控制器的方法 -(void)saveViewController:(UIViewController *) aUIViewController ToDic:(EnViewControllersId) aViewControllersId; //推下向下一級視圖控制器 -(void)pushViewcController:(EnViewControllersId) aViewControllersId; //返回上一級視圖控制器 -(void)popViewController:(EnViewControllersId) aViewControllersId; @end
// // BaseContentViewController.m // NewPICCProject // // Created by 杜甲 on 14-1-26. // Copyright (c) 2014年 杜甲. All rights reserved. // #import "BaseContentViewController.h" typedef enum { NomalType = 0, //不同形式切換 PushType ,//推下一級 PopTYpe//返回上一級 }ChangeType; @interface BaseContentViewController () @end @implementation BaseContentViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } -(id)init { if (self = [super init]) { //self.view.backgroundColor = [UIColor clearColor]; self.m_DicControllers = [[NSMutableDictionary alloc] init]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } //保存新創建的界面到字典中,參數:新創建的視圖控制器,對應的Id -(void)saveViewController:(UIViewController *) aUIViewController ToDic:(EnViewControllersId) aViewControllersId { NSInteger nNewViewControllerId = aViewControllersId; NSString *pStrNewKeyOfViewControllerId = [NSString stringWithFormat:@"%d", nNewViewControllerId]; [self.m_DicControllers setObject:aUIViewController forKey:pStrNewKeyOfViewControllerId]; } //根據ID獲取效應的視圖控制器 -(UIViewController*)getViewController:(EnViewControllersId) aViewControllersId { NSString* vcId = [NSString stringWithFormat:@"%d",aViewControllersId]; return [self.m_DicControllers objectForKey:vcId]; } //界面切換的方法 -(void)changeViewControllerToCenter:(EnViewControllersId) aViewControllersId { NSString* pStrCurrentKeyOfViewControllerId = [NSString stringWithFormat:@"%d",self.m_nCurrentViewControllerID]; NSString* pStrNewKeyOfViewControllerId = [NSString stringWithFormat:@"%d",aViewControllersId]; if (pStrCurrentKeyOfViewControllerId == nil) { NSLog(@"pStrCurrentKeyOfViewControllerId == nil"); } else { UIViewController* pCurrentViewController = [self.m_DicControllers objectForKey:pStrCurrentKeyOfViewControllerId]; NSLog(@"%@",pCurrentViewController); UIViewController* pNewViewController = [self.m_DicControllers objectForKey:pStrNewKeyOfViewControllerId]; NSLog(@"%@",pNewViewController); [pCurrentViewController.view removeFromSuperview]; [self.view addSubview:pNewViewController.view]; } self.m_nCurrentViewControllerID = aViewControllersId; } -(void)pushViewcController:(EnViewControllersId) aViewControllersId { NSString* pStrCurrentKeyOfViewControllerId = [NSString stringWithFormat:@"%d",self.m_nCurrentViewControllerID]; NSString* pStrNewKeyOfViewControllerId = [NSString stringWithFormat:@"%d",aViewControllersId]; if (pStrCurrentKeyOfViewControllerId == nil) { NSLog(@"pStrCurrentKeyOfViewControllerId == nil"); } else { UIViewController* pCurrentViewController = [self.m_DicControllers objectForKey:pStrCurrentKeyOfViewControllerId]; NSLog(@"%@",pCurrentViewController); UIViewController* pNewViewController = [self.m_DicControllers objectForKey:pStrNewKeyOfViewControllerId]; NSLog(@"%@",pNewViewController); pNewViewController.view.frame = CGRectMake(self.view.frame.size.width, pNewViewController.view.frame.origin.y, pNewViewController.view.frame.size.width, pNewViewController.view.frame.size.height); [self.view addSubview:pNewViewController.view]; [UIView animateWithDuration:0.25f animations:^{ pNewViewController.view.frame = CGRectMake(0, pNewViewController.view.frame.origin.y, pNewViewController.view.frame.size.width, pNewViewController.view.frame.size.height); pCurrentViewController.view.frame = CGRectMake(-self.view.frame.size.width, pNewViewController.view.frame.origin.y, pNewViewController.view.frame.size.width, pNewViewController.view.frame.size.height); } completion:^(BOOL finished) { [pCurrentViewController.view removeFromSuperview]; }]; } self.m_nCurrentViewControllerID = aViewControllersId; } -(void)popViewController:(EnViewControllersId) aViewControllersId { NSString* pStrCurrentKeyOfViewControllerId = [NSString stringWithFormat:@"%d",self.m_nCurrentViewControllerID]; NSString* pStrNewKeyOfViewControllerId = [NSString stringWithFormat:@"%d",aViewControllersId]; if (pStrCurrentKeyOfViewControllerId == nil) { NSLog(@"pStrCurrentKeyOfViewControllerId == nil"); } else { UIViewController* pCurrentViewController = [self.m_DicControllers objectForKey:pStrCurrentKeyOfViewControllerId]; NSLog(@"%@",pCurrentViewController); UIViewController* pNewViewController = [self.m_DicControllers objectForKey:pStrNewKeyOfViewControllerId]; NSLog(@"%@",pNewViewController); pNewViewController.view.frame = CGRectMake(-self.view.frame.size.width, pNewViewController.view.frame.origin.y, pNewViewController.view.frame.size.width, pNewViewController.view.frame.size.height); [self.view addSubview:pNewViewController.view]; [UIView animateWithDuration:0.25f animations:^{ pNewViewController.view.frame = CGRectMake(0, pNewViewController.view.frame.origin.y, pNewViewController.view.frame.size.width, pNewViewController.view.frame.size.height); pCurrentViewController.view.frame = CGRectMake(self.view.frame.size.width, pNewViewController.view.frame.origin.y, pNewViewController.view.frame.size.width, pNewViewController.view.frame.size.height); } completion:^(BOOL finished) { [pCurrentViewController.view removeFromSuperview]; }]; } self.m_nCurrentViewControllerID = aViewControllersId; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
用法就是直接繼承,先使用
//存儲視圖控制器的方法
-(void)saveViewController:(UIViewController *) aUIViewController
ToDic:(EnViewControllersId) aViewControllersId;
方法將視圖控制器保存。
之後就可以用:
//推下向下一級視圖控制器 -(void)pushViewcController:(EnViewControllersId) aViewControllersId; //返回上一級視圖控制器 -(void)popViewController:(EnViewControllersId) aViewControllersId;兩個方法切換視圖控制器了。