【问题标题】:popToViewController is not working but popViewController doespopToViewController 不工作,但 popViewController 工作
【发布时间】:2017-09-07 09:10:37
【问题描述】:
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 时。它上面有一个视图。请帮忙

【问题讨论】:

  • 可能是两个对象在推送和弹出时都不相同。能否请您显示您推送的位置和弹出位置的代码。

标签: ios swift uiviewcontroller uinavigationcontroller


【解决方案1】:

试试这个:-

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
    }
}

确保您的视图控制器已添加到导航控制器堆栈中。

【讨论】:

  • 谢谢快速回答。一个问题,我正在尝试传递数据,但无法使用您的 code.ex 进行传递 - 我正在尝试的代码是 obj.data = data。
  • 使用 obj 投射控制器并传递数据。
  • 你能告诉我怎么做吗当我使用 let vc2 = self?.storyboard?.instantiateViewController(withIdentifier: "TestViewController") as! TestViewController 然后使用 vc2,data = data
  • 请点赞我的问题,让更多人有机会查看您的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-03
相关资源
最近更新 更多