【问题标题】:Move to root ViewController from popup view swift从弹出视图快速移动到根 ViewController
【发布时间】:2020-07-25 06:36:46
【问题描述】:

我已经看到了类似的问题,但无法找到合适的答案。我现在显示一个视图,我想在单击按钮时从该视图移至根视图控制器。 self.navigationController?.popToRootViewController(animated: true) 不工作。

也试过下面的代码,但它不适合我。

UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: true, completion: nil)

if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.window?.rootViewController?.dismiss(animated: true, completion: nil)
(appDelegate.window?.rootViewController as? UINavigationController)?.popToRootViewController(animated: true)
}

【问题讨论】:

  • 你确定 self.navigationController 不是 nil 吗?
  • self.navigationController 为零。
  • 那不好,你得阅读一些关于如何使用 NavigationController 的文章或文档。

标签: swift view popup


【解决方案1】:

好的,这里的解决方案是创建一个协议,该协议向符合它的任何人抛出一个委托,以便控制器可以知道按钮被单击,因此您的父控制器具有导航控制器,它会将您弹出到根视图控制器

在你的 popvc.swift 中

    protocol PopupDelegate : AnyObject{
        func buttonTapped(_ status : Bool)
    }
    
    class popUpVc: UIViewController {
    
        weak var delegate : PopupDelegate?
    
     @IBAction func btnLoginAction(_ sender: Any) {
    
      self.delegate?.buttonTapped(true)
      self.dismiss(animated: true, completion: nil)
    
     }
    
    }

现在在显示弹出窗口的父视图控制器中

        let vc = self.storyBoard.instantiateViewController(withIdentifier: "WaitingVc") as! WaitingVc
        vc.delegate = self
        self.present(vc, animated: true, completion: nil)

遵守这样的协议

extension yourParentVC : PopupDelegate{

func PopupDelegate(_ status: Bool) {
if status{
  self.navigationController?.popToRootViewController(animated: true)
   }

}

使用时记住

self.delegate?.buttonTapped(true)

parentVc 会弹出你,这样你就可以使用延迟或其他东西,或者可以在关闭完成时调用线路,以免错过动画。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-07
    • 2020-10-21
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 2016-12-15
    相关资源
    最近更新 更多