提问者:小点点

快速返回到预览UINavigationController


我的VC就像一个“出了问题”。我创建的这个VC像一个单独的VC(没有故事板),我想在我想要的地方展示它。但在“something wating expect”视图控制器中,我有一个按钮“refreship”。当用户点击此按钮时,他必须返回。

当我在解析Json或类似的事情时遇到一些问题时,我会调用something went English“View Controller,如下所示:

let navController = UINavigationController()

        navController.pushViewController(SomethingWentWorngVC(nibName: "SomethingWentWorngView", bundle: nil), animated: false)

        window?.rootViewController = navController
        window?.makeKeyAndVisible()

此外,我还有获取的扩展

extension UIViewController {
     var appDelegate: AppDelegate {
     return UIApplication.shared.delegate as! AppDelegate
 }
 
 var sceneDelegate: SceneDelegate? {
     guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
         let delegate = windowScene.delegate as? SceneDelegate else { return nil }
      return delegate
 }
}

extension UIViewController {
 var window: UIWindow? {
     if #available(iOS 13, *) {
         guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
             let delegate = windowScene.delegate as? SceneDelegate, let window = delegate.window else { return nil }
                return window
     }
     
     guard let delegate = UIApplication.shared.delegate as? AppDelegate, let window = delegate.window else { return nil }
     return window
 }
}

中,我有返回按钮

@IBAction func refreshAction(_ sender: Any) {
    self.navigationController?.popToRootViewController(animated: false)

}

但它不起作用


共1个答案

匿名用户

你可以用这种替代的方式来使用它,首先你需要在appdelegate中使用tag创建公共代码,然后你需要做addsubview到window,例如,

作秀

func showWentWrongScreen(){
        let getVC = SomethingWentWorngVC
                      if let getWindow = self.window {
                          getVC.view.tag = 501
                          getVC.view.frame = getWindow.bounds
                          getWindow.addSubview(getVC.view)
                      }
    }

用于删除

func removeWentWrongScreen(){
    if let getWindow = self.window, let getWentWrongView = getWindow.viewWithTag(501){
        getWentWrongView.removeFromSuperview()
    }
}

null