專案裡用久了系統自帶的 UINavigationController,就會發現一個麻煩:導航列的樣式很難改。想換個背景、調整字型、加上自訂的轉場節奏,往往要跟系統的預設行為纏鬥半天。與其硬改,不如自己攢一套輕量的切換機制——這篇文章記錄的就是這樣一個做法。
老實說,好不好用這種事很主觀,一部分原因大概是「自己寫的總覺得順手」。但這套思路確實把視圖控制器的管理、快取與轉場動畫都收攏到一個基類裡,後續擴充相當直接。
核心只有三件事:
// BaseContentViewController.h
#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
#import "BaseContentViewController.h"
typedef enum {
NomalType = 0, // 直接切換
PushType, // 前推
PopTYpe // 返回
} ChangeType;
@implementation BaseContentViewController
- (id)init {
if (self = [super init]) {
self.m_DicControllers = [[NSMutableDictionary alloc] init];
}
return self;
}
// 以 ID 為 key 存進字典
- (void)saveViewController:(UIViewController *)aUIViewController
ToDic:(EnViewControllersId)aViewControllersId {
NSString *key = [NSString stringWithFormat:@"%d", aViewControllersId];
[self.m_DicControllers setObject:aUIViewController forKey:key];
}
- (UIViewController *)getViewController:(EnViewControllersId)aViewControllersId {
NSString *key = [NSString stringWithFormat:@"%d", aViewControllersId];
return [self.m_DicControllers objectForKey:key];
}
// 直接切換,不做動畫
- (void)changeViewControllerToCenter:(EnViewControllersId)aViewControllersId {
UIViewController *current = [self.m_DicControllers objectForKey:
[NSString stringWithFormat:@"%d", self.m_nCurrentViewControllerID]];
UIViewController *next = [self.m_DicControllers objectForKey:
[NSString stringWithFormat:@"%d", aViewControllersId]];
[current.view removeFromSuperview];
[self.view addSubview:next.view];
self.m_nCurrentViewControllerID = aViewControllersId;
}
// 新視圖由右側滑入,舊視圖向左退出
- (void)pushViewcController:(EnViewControllersId)aViewControllersId {
UIViewController *current = [self.m_DicControllers objectForKey:
[NSString stringWithFormat:@"%d", self.m_nCurrentViewControllerID]];
UIViewController *next = [self.m_DicControllers objectForKey:
[NSString stringWithFormat:@"%d", aViewControllersId]];
next.view.frame = CGRectMake(self.view.frame.size.width, next.view.frame.origin.y,
next.view.frame.size.width, next.view.frame.size.height);
[self.view addSubview:next.view];
[UIView animateWithDuration:0.25f animations:^{
next.view.frame = CGRectMake(0, next.view.frame.origin.y,
next.view.frame.size.width, next.view.frame.size.height);
current.view.frame = CGRectMake(-self.view.frame.size.width, next.view.frame.origin.y,
next.view.frame.size.width, next.view.frame.size.height);
} completion:^(BOOL finished) {
[current.view removeFromSuperview];
}];
self.m_nCurrentViewControllerID = aViewControllersId;
}
// 新視圖由左側滑入,舊視圖向右退出
- (void)popViewController:(EnViewControllersId)aViewControllersId {
UIViewController *current = [self.m_DicControllers objectForKey:
[NSString stringWithFormat:@"%d", self.m_nCurrentViewControllerID]];
UIViewController *next = [self.m_DicControllers objectForKey:
[NSString stringWithFormat:@"%d", aViewControllersId]];
next.view.frame = CGRectMake(-self.view.frame.size.width, next.view.frame.origin.y,
next.view.frame.size.width, next.view.frame.size.height);
[self.view addSubview:next.view];
[UIView animateWithDuration:0.25f animations:^{
next.view.frame = CGRectMake(0, next.view.frame.origin.y,
next.view.frame.size.width, next.view.frame.size.height);
current.view.frame = CGRectMake(self.view.frame.size.width, next.view.frame.origin.y,
next.view.frame.size.width, next.view.frame.size.height);
} completion:^(BOOL finished) {
[current.view removeFromSuperview];
}];
self.m_nCurrentViewControllerID = aViewControllersId;
}
@end
讓自己的容器控制器繼承 BaseContentViewController,先把要用到的子視圖控制器全部註冊進來:
[self saveViewController:homeVC ToDic:HomeVC_Id]; [self saveViewController:mainVC ToDic:ViewController_Id];
之後要切換,就只要呼叫:
// 前推 [self pushViewcController:ComparisonVC_Id]; // 返回 [self popViewController:HomeVC_Id];
因為所有子控制器都被字典持有,切換時不必反覆建立物件,也不會每次都重新拉資料——代價是記憶體佔用會隨註冊數量線性增加,頁面多的時候記得權衡。