這里要達到的效果是A.Nav - push -> B B - push -> C C-pop- >A
以及 如果C不popA 則 能 C -push - >B B和C相互都能push出來 最后他們pop 都能pop 到A 的效果.而且系統(tǒng)自帶的手勢也能滑到A控制器.
通常情況下,我們的控制器之間的跳轉(zhuǎn)是這樣的:
A -present-> B B -dismiss-> A```
相互之間都是有層級關(guān)系和順序的
首先導(dǎo)航控制器push和pop 是棧先進后出原理
A-push->B B-push->C 之后 控制器的子控制器數(shù)組為(A, B, C)
C- pop->A 則要將數(shù)組中的B移除, 達到想要的效果
直接上代碼
AppDelegate.m中
import "AppDelegate.h"
import "ViewController.h"
import "qqViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];qqViewController *view = [qqViewController new];
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:view];
self.window.rootViewController = navi;
return YES;
}
A控制器qqViewController.m中
import "qqViewController.h"
import "hhViewController.h"
@interface qqViewController ()
@end
@implementation qqViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"首頁";
self.view.backgroundColor = [UIColor blackColor];
[self.view addSubview:[self createButton]];
} - (UIButton *)createButton {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.backgroundColor = [UIColor redColor];
button.frame = CGRectMake(140, 100, 100, 30);
[button setTitle:@"點擊" forState:UIControlStateNormal];
[button addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
return button;
} - (void)push {
hhViewController *v = [hhViewController new];
[self.navigationController pushViewController:v animated:YES];
}
B控制器hhViewController.m中
import "hhViewController.h"
import "ccViewController.h"
@interface hhViewController ()
@end
@implementation hhViewController
- (void)viewWillAppear:(BOOL)animated {
[self.navigationController setViewControllers:@[self.navigationController.childViewControllers.firstObject, self]];
// 在B控制器將要出現(xiàn)的時候 偷梁換柱 將導(dǎo)航控制器的ViewControllers中的元素修改掉 達到C-pop 回去的是A控制器
} - (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"第二";
self.view.backgroundColor = [UIColor redColor];
[self.view addSubview:[self createButton]];
} - (void)push {
ccViewController *v = [ccViewController new];
v.view.backgroundColor = [UIColor grayColor];
[self.navigationController pushViewController:v animated:YES];
} - (UIButton *)createButton {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.backgroundColor = [UIColor greenColor];
button.frame = CGRectMake(140, 200, 100, 30);
[button setTitle:@"點擊" forState:UIControlStateNormal];
[button addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
return button;
}
C控制器ccViewController.m中
import "ccViewController.h"
import "hhViewController.h"
@interface ccViewController ()
@end
@implementation ccViewController
- (void)viewWillAppear:(BOOL)animated {
[self.navigationController setViewControllers:@[self.navigationController.childViewControllers.firstObject, self]];
// 在C控制器將要出現(xiàn)的時候 偷梁換柱 將導(dǎo)航控制器的ViewControllers中的元素修改掉 達到C-push->B B-pop->A
} - (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"第三";
[self.view addSubview:[self createButton]];
} - (void)push {
hhViewController *v = [hhViewController new];
v.view.backgroundColor = [UIColor redColor];
[self.navigationController pushViewController:v animated:YES];
} - (UIButton *)createButton {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.backgroundColor = [UIColor greenColor];
button.frame = CGRectMake(100, 200, 100, 30);
[button setTitle:@"點擊" forState:UIControlStateNormal];
[button addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
return button;
}
看看運行效果吧


以下可以選擇性適當(dāng)忽略:
禁止pop后,**< Back**中的**<**會置灰,文字**Back**卻不會(可能又是Apple Inc.的小Bug)。解決方法很簡單:在vc中調(diào)用以下兩句代碼。
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self.navigationController setNavigationBarHidden:NO animated:YES];
比如,用戶點擊backBarButtonItem
時,我提示用戶是否繼續(xù)離開,如果用戶選擇**OK**則**POP**離開,如果用戶選擇**NO**則留在本頁并執(zhí)行上面兩句,使**<Back**中的**<**恢復(fù)正常顏色。
好了??葱Ч到y(tǒng)導(dǎo)航自帶的back在push到C控制器時 back上的第二變成首頁 效果還在上面 要怎么消除這個小bug呢,就要看下面的代碼啦
因為大部分的App都會給導(dǎo)航設(shè)置標題,所以也就要自定義leftBarButtonItem了、但是 大家都知道自定義NavigationBar會使系統(tǒng)自帶滑UIScreenEdgePanGestureRecognizer這個手勢失效.要怎么修改,看下面的代碼把
首先 我們自定義一個導(dǎo)航控制器繼承UINavigationController
在NavigationController.m中代碼如下
import "NavigationController.h"
@interface NavigationController ()<UIGestureRecognizerDelegate>
@end
@implementation NavigationController
- (void)viewDidLoad {
[super viewDidLoad];
// 如果想把導(dǎo)航系統(tǒng)手勢改成全屏滑的時候 則是注釋的部分
// // 設(shè)置代理
// id target = self.interactivePopGestureRecognizer.delegate;
// // 創(chuàng)建手勢
// UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
// // 設(shè)置pan手勢的代理
// pan.delegate = self;
// // 添加手勢
// [self.view addGestureRecognizer:pan];
// // 將系統(tǒng)自帶的手勢覆蓋掉
// self.interactivePopGestureRecognizer.enabled = NO;
// 在這里我們還是保留系統(tǒng)自帶滑動UIScreenEdgePanGestureRecognizer
// 設(shè)置代理
self.interactivePopGestureRecognizer.delegate = self;
}
// 表示的意思是:當(dāng)前控制器是根控制器,那么就不接收觸摸事件,只有當(dāng)不是根控制器時才需要接收事件.
pragma mark - UIGestureRecognizerDelegate
// 是否觸發(fā)手勢
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return self.childViewControllers.count > 1;
}
建好之后 回到AppDelegate.m
import "NavigationController.h"
將NavigationController設(shè)置成根視圖的導(dǎo)航
在B控制器hhViewController.m中
添加自定義item
在viewDidLoad方法中添加
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(pop)];
方法實現(xiàn)
- (void)pop {
[self.navigationController popViewControllerAnimated:YES];
}
在C控制器ccViewController.m中
添加自定義item
在viewDidLoad方法中添加
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(pop)];
方法實現(xiàn)
- (void)pop {
[self.navigationController popViewControllerAnimated:YES];
}
可以看效果啦

代碼如果想簡化 直接創(chuàng)建基類繼承UIViewController的BaseViewController
將相同的代碼都放到里面 在創(chuàng)建A\B\C控制器的時候繼承BaseViewController,直接調(diào)用父類的方法就行.這里就不演示啦.
附上Demo:https://github.com/catcups/pop