Ios popToViewController不工作,但popViewController工作

Ios popToViewController不工作,但popViewController工作,ios,swift,uiviewcontroller,uinavigationcontroller,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) 我正在像这样推这个viewCo

使用此代码时

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)
我正在像这样推这个viewController

self?.navigationController?.popViewController(animated: true)
我不确定,推送视图意味着当我尝试执行popToViewController时。它上面有一个视图。请帮助

尝试以下操作:-

navigationController?.pushViewController(vc2, animated: true)

确保已将视图控制器添加到navigationcontroller堆栈上。

可能是两个对象在推送和弹出时不相同。你能显示一下你推的地方和弹出的地方的代码吗。Thnx可以快速回答。有一个问题,我正在尝试传递数据,但无法使用您的代码来传递数据。ex-我正在尝试的代码是obj.data=data。使用obj投射您的控制器并传递数据。您能告诉我如何使用let vc2=self?.storyboard?.instanceeviewcontrollerwhiteIdentifier:TestViewController As!TestViewController,然后使用vc2,data=dataPlease upvote我的问题,让更多的人有机会检查你的答案
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
    }
}