【问题标题】:ViewController is getting called multiple times from my rootViewControllerViewController 从我的 rootViewController 被多次调用
【发布时间】:2017-08-24 06:09:16
【问题描述】:

我对@9​​87654321@ 堆栈有点困惑。 我正在通过我的rootViewController("ViewController") 显示我的UIViewController("SubmitRequestViewController")

下面是我的代码:

let controller = SubmitRequestViewController.init(nibName: "SubmitRequestViewController", bundle: nil)
    self.navigationController?.pushViewController(controller, animated: true) 

现在我只是想从SubmitRequestViewController 回到我的rootViewController

//Home Button
@objc fileprivate func backHome() {
    let _ = self.navigationController?.popToRootViewController(animated: true)
}

//Back Button
@objc fileprivate func backButton() {
    let _ = self.navigationController?.popViewController(animated: true)
}

如果我再次访问我的“SubmitRequestViewController”,它会接到两次电话。

所以这会一直持续下去。如果我重复上述步骤,它将多次调用。

现在我得到的是:-

navigationController.viewControllers.count 不断增加。

我正在尝试弹出到navigationController,但它仍然存在。

我也试过这个:-

self.navigationController?.dismiss(animated: true, completion: nil)

实际上,我在 DrawerViewController 类中触发了一个通知:-

//Submit Request
else if itemArray[indexPath.row].contains("SUBMIT REQUEST") == true {
    tableView.deselectRow(at: indexPath, animated: false)
    self.dismiss(animated: true, completion: nil)
    NotificationCenter.default.post(name: Notification.Name(rawValue : AssessNowKyes.submitRequest), object: nil)
}

我在我的 ViewControllerClass 中调用它:-

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(submitRequestClicked), name: Notification.Name(rawValue : AssessNowKyes.submitRequest), object: nil)
}

//Submit Request Clicked
@objc fileprivate func submitRequestClicked() {
    let controller = SubmitRequestViewController.init(nibName: "SubmitRequestViewController", bundle: nil)
    self.navigationController?.pushViewController(controller, animated: true)
}

我正在做。我不知道我也必须删除观察者吗?因为我也试过了。它不工作。

【问题讨论】:

  • 你可以尝试在主线程上运行popviewcontroller方法吗?
  • remove let _ = , self.navigationController?.popToRootViewController(animated: true) 是需要的
  • 你的意思只是这个 self.navigationController?.popViewController(animated: true) ?
  • 我试过@Basheer。但同样的问题。 DispatchQueue.main.async { self.navigationController?.popViewController(animated: true) }
  • 也许你可以在 SubmitRequestViewController 中检查 deinit。

标签: ios swift uinavigationcontroller navigation pushviewcontroller


【解决方案1】:

我无法再次出现此问题。

我猜你必须触发两次 push 动作。

  • SubmitRequestViewController 上的viewDidLoad 上添加断点以检查viewDidLoad 是否被调用了两次
  • 检查是否在情节提要上触发它并再次编程
  • 移除 nib 并重新创建以检查这是否是 Xcode 的错误

【讨论】:

  • 好的,我会尝试一次。谢谢
【解决方案2】:

检查您在哪里调用它。你打电话给ViewWillAppear吗?

如果是,这是错误的。

let controller = SubmitRequestViewController.init(nibName: "SubmitRequestViewController", bundle: nil)
    self.navigationController?.pushViewController(controller, animated: true) 

【讨论】:

  • 你的意思是我在 ViewWillAppear 中写它?
  • 是的,我的意思是当你展示这个 rootController 时触发的任何函数
  • 实际上我正在做的是我正在触发通知。我会关上抽屉,然后发出通知并调用它。
  • 你在哪里触发这个通知,分享一下吧
  • 是的,我会分享。谢谢
猜你喜欢
  • 1970-01-01
  • 2010-11-08
  • 2018-05-25
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-15
相关资源
最近更新 更多