这里要达到的效果是A.Nav - push -> B B - push -> C C-pop- >A
以及 如果C不popA 则 能 C -push - >B B和C相互都能push出来 最后他们pop 都能pop 到A 的效果.而且系统自带的手势也能滑到A控制器.
通常情况下,我们的控制器之间的跳转是这样的:
A -present-> B B -dismiss-> A```
相互之间都是有层级关系和顺序的
首先导航控制器push和pop 是栈先进后出原理
A-push->B B-push->C 之后 控制器的子控制器数组为(A, B, C)
C- pop->A 则要将数组中的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控制器将要出现的时候 偷梁换柱 将导航控制器的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控制器将要出现的时候 偷梁换柱 将导航控制器的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;
}
看看运行效果吧
![qh_1.gif](http://upload-images.jianshu.io/upload_images/1788484-a951085ecd4413ea.gif?imageMogr2/auto-orient/strip)
![qh_2.gif](http://upload-images.jianshu.io/upload_images/1788484-4fc594dfdbff77ff.gif?imageMogr2/auto-orient/strip)
以下可以选择性适当忽略:
禁止pop后,**< Back**中的**<**会置灰,文字**Back**却不会(可能又是Apple Inc.的小Bug)。解决方法很简单:在vc中调用以下两句代码。
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self.navigationController setNavigationBarHidden:NO animated:YES];
比如,用户点击backBarButtonItem
时,我提示用户是否继续离开,如果用户选择**OK**则**POP**离开,如果用户选择**NO**则留在本页并执行上面两句,使**<Back**中的**<**恢复正常颜色。
好了。看效果系统导航自带的back在push到C控制器时 back上的第二变成首页 效果还在上面 要怎么消除这个小bug呢,就要看下面的代码啦
因为大部分的App都会给导航设置标题,所以也就要自定义leftBarButtonItem了、但是 大家都知道自定义NavigationBar会使系统自带滑UIScreenEdgePanGestureRecognizer这个手势失效.要怎么修改,看下面的代码把
首先 我们自定义一个导航控制器继承UINavigationController
在NavigationController.m中代码如下
import "NavigationController.h"
@interface NavigationController ()<UIGestureRecognizerDelegate>
@end
@implementation NavigationController
- (void)viewDidLoad {
[super viewDidLoad];
// 如果想把导航系统手势改成全屏滑的时候 则是注释的部分
// // 设置代理
// id target = self.interactivePopGestureRecognizer.delegate;
// // 创建手势
// UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
// // 设置pan手势的代理
// pan.delegate = self;
// // 添加手势
// [self.view addGestureRecognizer:pan];
// // 将系统自带的手势覆盖掉
// self.interactivePopGestureRecognizer.enabled = NO;
// 在这里我们还是保留系统自带滑动UIScreenEdgePanGestureRecognizer
// 设置代理
self.interactivePopGestureRecognizer.delegate = self;
}
// 表示的意思是:当前控制器是根控制器,那么就不接收触摸事件,只有当不是根控制器时才需要接收事件.
pragma mark - UIGestureRecognizerDelegate
// 是否触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return self.childViewControllers.count > 1;
}
建好之后 回到AppDelegate.m
import "NavigationController.h"
将NavigationController设置成根视图的导航
在B控制器hhViewController.m中
添加自定义item
在viewDidLoad方法中添加
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(pop)];
方法实现
- (void)pop {
[self.navigationController popViewControllerAnimated:YES];
}
在C控制器ccViewController.m中
添加自定义item
在viewDidLoad方法中添加
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(pop)];
方法实现
- (void)pop {
[self.navigationController popViewControllerAnimated:YES];
}
可以看效果啦
![qh_3.gif](http://upload-images.jianshu.io/upload_images/1788484-f54b01514fb4bc02.gif?imageMogr2/auto-orient/strip)
代码如果想简化 直接创建基类继承UIViewController的BaseViewController
将相同的代码都放到里面 在创建A\B\C控制器的时候继承BaseViewController,直接调用父类的方法就行.这里就不演示啦.
附上Demo:https://github.com/catcups/pop