iOS自訂類似導航的視圖切換架構

來源:互聯網
上載者:User

在項目開發中使用iOS內建的導航,因為其樣式修改起來比較麻煩,這裡我自己寫了一個類似的切換方式。我自己覺得還挺好用的,可能是因為代碼是自己寫的吧。。


先把代碼附上:

BaseContentViewController.h

////  BaseContentViewController.h//  NewPICCProject////  Created by 杜甲 on 14-1-26.//  Copyright (c) 2014年 杜甲. All rights reserved.//#import 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

////  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;
兩個方法切換視圖控制器了。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.