以前一直有个很疑惑的问题没有搞清楚
关于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];
1 2 3 | if let index = self.navigationController?.viewControllers.index(of: self) { self.navigationController?.viewControllers.remove(at: index) } |
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时候,重构顺序
1 2 3 4 5 | 因为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的地方需要再次重构顺序)
1 2 3 4 5 6 7 8 | 在界面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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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 ) } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现