Sliding back to the designated controller

Today, I chatted with my friend and my friend said a strange requirement: Manual sliding back needs to jump to the specified page.
I thought about the following three ideas:
1. Custom navigation control rewrite the popViewControllerAnimated method, in the method, next article
2. Exchange Navigation controller sub-control sequence
3. Directly pop the controller between the current controller and the target controller. After
careful consideration, the third method is finally used. The
first method is quite troublesome to implement.
The second method is There is a small problem, you can think about it yourself. The
third implementation is relatively simple.

//在需要侧滑到指定控制器的控制器的 view 加载完毕后偷偷将当前控制器与目标控制器之间的所有控制器出栈
  # 1. 获取当行控制器所有子控制器
  NSMutableArray <UIViewController *>* tmp = self.navigationController.viewControllers.mutableCopy;
  # 2. 获取目标控制器索引
  UIViewController * targetVC = nil;
    for (NSInteger i = 0 ; i < tmp.count; i++) {
        
        UIViewController * vc = tmp[i];
        if ([vc isKindOfClass:NSClassFromString(@"TargetViewController")])
        {
            targetVC = vc;
            // 也可在此直接获取 i 的数值
            break;
        }
    }
  NSInteger index = [tmp indexOfObject:targetVC];
  # 3. 移除目标控制器与当前控制器之间的所有控制器
  NSRange  range = NSMakeRange(index + 1, tmp.count - 1 - (index + 1));
    
  [tmp removeObjectsInRange:range];
  # 4. 重新赋值给导航控制器
  self.navigationController.viewControllers = [tmp copy];

The above method needs to be executed at an appropriate time to avoid not taking effect.
Transfer from: https://www.jianshu.com/p/66c41c6d9c8f

Guess you like

Origin blog.csdn.net/qq_28285625/article/details/107979327