popToViewController不起作用,但popViewController没有

时间:2017-09-07 09:10:38

标签: ios swift uiviewcontroller uinavigationcontroller

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.'

使用此代码时

self?.navigationController?.popToViewController(vc2, animated: true)

但是当我尝试简单地弹出它的工作

self?.navigationController?.popViewController(animated: true)

我正在推动这个viewController,如

navigationController?.pushViewController(vc2, animated: true)

我不确定,推送视图意味着当我尝试popToViewController时。它有一个视图。请帮忙

1 个答案:

答案 0 :(得分:4)

试试这个: -

 for obj in (self.navigationController?.viewControllers)! {
            if obj is TestViewController {
  let vc2: TestViewController =  obj as! TestViewController
   vc2.data = data
                _ =  
 self.navigationController?.popToViewController(vc2, animated: true)
  break
            }
        }

确保在navigationcontroller堆栈上添加了视图控制器。