在项目开发中使用iOS自带的导航,因为其样式修改起来比较麻烦,这里我自己写了一个类似的切换方式。我自己觉得还挺好用的,可能是因为代码是自己写的吧。。
先把代码附上:
BaseContentViewController.h
// // BaseContentViewController.h // NewPICCProject // // Created by 杜甲 on 14-1-26. // Copyright (c) 2014年 杜甲. All rights reserved. // #import <UIKit/UIKit.h> typedef 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;两个方法切换视图控制器了。
原文:http://blog.csdn.net/qqmcy/article/details/19158371