美文网首页
iOS UINavigationController 推到下一个

iOS UINavigationController 推到下一个

作者: xx鹤xx | 来源:发表于2017-08-09 11:24 被阅读0次

以下为swift 3 版本

使用场景:如申请流程有多个页面分别

  1. 详情页
  2. 阶段1
  3. 阶段2
  4. 结果
    都使用 .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

相关文章

网友评论

      本文标题:iOS UINavigationController 推到下一个

      本文链接:https://www.haomeiwen.com/subject/cgefrxtx.html