iOS - push 或 pop或点击导航栏返回pop指定导航控制器

以前一直有个很疑惑的问题没有搞清楚

关于ios中 viewcontroller的跳转问题,其中有一种方式是采用navigationController pushViewController 的方法,比如我从主页面跳转到了一级页面,又从一级页面跳转到了二级页面,然后从二级页面跳转到了三级页面,依次类推。,如果一级一级的返回我知道是没有问题的,调用navigationController popViewControllerAnimated就行了。。但是某些情况下我可能想要马上回到主页面,而不是一级一级的返回(如果有很多层会很累的),那该怎么办呢?

不是点击返回,点击指定btn返回到指定界面

1.返回根页面vc用 :

[self.navigationController popToRootViewController]

2.返回指定的某个vc用下面(通过index定位) 

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];

3.(通过class定位),当前控制器有这个控制器存在

for (UIViewController *controller in self.navigationController.viewControllers) {

  if ([controller isKindOfClass:[你要跳转到的Controller class]]) {

    [self.navigationController popToViewController:controller animated:YES];

    }

}

重点 点击返回pop到指定控制器,当前控制器可能根本就没创建过,或者返回顺序和之前进来的顺序完全不一致,下面1代表根控制器

1.重构当行控制器viewControllers顺序 :需注意一个事项,没有的vc(根本就不存在的vc,不是push的vc,)一定要重写下面的返回,因为Vc创建的时候根本不是push进来的,目前这个方法可以解决xib,sb,纯代码创建的VC

vc.navigationItem.backBarButtonItem = UIBarButtonItem(title: "返回或者("<" 或 "指定的返回图片" 或 "文字)", style: .plain, target: self, action: nil)

 1.1简单型 push顺序本来是 1234,返回顺序425631,需要在第3push第4时候,重构顺序

  因为vc5, vc6 不存在 所以需要加上代码
  vc5.navigationItem.backBarButtonItem = UIBarButtonItem(title: "返回或者("<" 或 "指定的返回图片" 或 "文字)", style: .plain, target: self, action: nil)
  vc6.navigationItem.backBarButtonItem = UIBarButtonItem(title: "返回或者("<" 或 "指定的返回图片" 或 "文字)", style: .plain, target: self, action: nil) 
  let newViewController: [UIViewController] = [vc4 , vc2, vc5, vc4, vc3, vc2, vc1]
  self?.navigationController?.setViewControllers(newViewController, animated: true)

  

1.2变态复杂性 push顺序123,返回顺序 341(在2push的地方重构viewControllers),但是在第3push到4的时候,返回顺序变了469231(在3push的地方再次重构顺序),第4push到第5的时候,返回变成了5283671(在4push的地方需要再次重构顺序)

  在界面2需要重构 , 因为vc4 不存在 所以需要加上代码
  vc4.navigationItem.backBarButtonItem = UIBarButtonItem(title: "返回或者("<" 或 "指定的返回图片" 或 "文字)", style: .plain, target: self, action: nil) 
  let newViewController: [UIViewController] = [vc3 , vc4, vc1]
  self?.navigationController?.setViewControllers(newViewController, animated: true)

 
 
  在界面3需要重构 , 因为vc6,9 不存在 所以需要加上代码
  vc6.navigationItem.backBarButtonItem = UIBarButtonItem(title: "返回或者("<" 或 "指定的返回图片" 或 "文字)", style: .plain, target: self, action: nil) 
  vc9.navigationItem.backBarButtonItem = UIBarButtonItem(title: "返回或者("<" 或 "指定的返回图片" 或 "文字)", style: .plain, target: self, action: nil) 
  let newViewController: [UIViewController] = [vc4, vc6, vc9, vc2, vc3,vc1]
  self?.navigationController?.setViewControllers(newViewController, animated: true)

  

1.3没有人性型, 和1.2一样,但是3这个界面有各个界面push进来,返回的时候多个界面需要重构。。。

例如:俩简单的,1根控制器,123->3961, 4532->23694

let controllers = self.navigationController.viewControllers

  for v in controllers{
    
    //123->3691
    if v is vc2(vc2是类名字), controllers.count == 2{
      let vc6 = vc6()
      vc6.navigationItem.backBarButtonItem = UIBarButtonItem(title: "返回或者("<" 或 "指定的返回图片" 或 "文字)", style: .plain, target: self, action: nil) 
      let vc9 = vc9()
      vc9.navigationItem.backBarButtonItem = UIBarButtonItem(title: "返回或者("<" 或 "指定的返回图片" 或 "文字)", style: .plain, target: self, action: nil) 
      let newViewController: [UIViewController] = [vc3, vc9, vc6, vc1]
      self?.navigationController?.setViewControllers(newViewController, animated: true)
    }else{
      self?.navigationController?.setViewControllers(newViewController, animated: true)
    }

    //4532->23694    
    if v is vc4(vc4是类名字),vc.count ==2{       
      let vc6 = vc6()
      vc6.navigationItem.backBarButtonItem = UIBarButtonItem(title: "返回或者("<" 或 "指定的返回图片" 或 "文字)", style: .plain, target: self, action: nil) 
      let vc9 = vc9()
      vc9.navigationItem.backBarButtonItem = UIBarButtonItem(title: "返回或者("<" 或 "指定的返回图片" 或 "文字)", style: .plain, target: self, action: nil) 
      let newViewController: [UIViewController] = [vc4, vc9, vc6, vc3]
      self?.navigationController?.setViewControllers(newViewController, animated: true)
    }else{
      self?.navigationController?.setViewControllers(newViewController, animated: true)
    }
   } 

  

猜你喜欢

转载自www.cnblogs.com/qingzZ/p/9253182.html