以下为swift 3 版本
使用场景:如申请流程有多个页面分别
- 详情页
- 阶段1
- 阶段2
- 结果
都使用 .pushViewController 推进
要求在结果页时需能 左滑/按回退到 #1 详情页, ( 使用 .popViewController )
此时可以在 #阶段2页 .push #结果页 时,使用如下写法
使用指针的版本:
let vc = ResultViewController()
if let nc = self?.navigationController {
nc.pushViewController(vc, animated: true)
withUnsafeMutablePointer(to: &vc.navigationController!.viewControllers) { v in
//移除倒数2,3 两个ViewController
v.pointee.removeSubrange((v.pointee.count - 3 ..< v.pointee.count - 1))
}
}
不使用指针的版本:
let vc = ResultViewController()
if let nc = self?.navigationController {
nc.pushViewController(vc, animated: true)
if let nvcs = vc.navigationController?.viewControllers {
//移除倒数2,3 两个ViewController
nvcs.removeSubrange((v.pointee.count - 3 ..< v.pointee.count - 1))
vc.navigationController?.viewControllers = nvcs
}
}
区别在于:因为获取的viewContollers数组是一个copy,在其内移除item不会影响到真正的vc.navigationController?.viewControllers,所以不使用指针的话,需要再把修改过的数组赋回给vc.navigationController?.viewControllers
网友评论