其实就是指定跳转位置,略过你栈中的一个或多个控制器。核心方法就是- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated;,只要在你的navigation.viewControllers中拿到你想要的控制器就可以了。以下代码可以略微参考一下,是自己写的navigation基类,实现点击返回的手动控制及位置指定
// .h
@property (nonatomic, assign) NSInteger targetIndex;
@property (nonatomic, copy) void(^backOperStandBy)(void(^handleBack)(BOOL back,NSInteger targetIndex),NSInteger operIndex);
// .m
- (void)sp_popViewController{
if (self.backOperStandBy != nil) {
__weak typeof(self) ws = self;
self.backOperStandBy(^(BOOL back, NSInteger targetIndex) {
if (back) {
ws.targetIndex = targetIndex;
[ws handlePopAction];
}
}, self.viewControllers.count-1);
} else {
[self handlePopAction];
}
}
- (void)handlePopAction
{
if (self.targetIndex == RootIndex) {
[self popToRootViewControllerAnimated:YES];
return;
}
if (self.targetIndex != 0 && self.targetIndex > 0) {
UIViewController *target = self.viewControllers[self.targetIndex];
self.targetIndex = 0;
if (target) {
[self popToViewController:target animated:YES];
}else{
[self popViewControllerAnimated:YES];
}
return;
}
// 普通情况
[self popViewControllerAnimated:YES];
}