Swift 从通知操作显示特定的视图控制器

Swift 从通知操作显示特定的视图控制器,swift,viewcontroller,appdelegate,unusernotificationcenter,usernotifications,Swift,Viewcontroller,Appdelegate,Unusernotificationcenter,Usernotifications,我正在处理本地通知,我正在尝试展示一个特定的viewController,但我尝试了我在这个论坛中发现的内容,我在中显示的视图中发现了一个不寻常的行为 以下是AppDelegate.swift的源代码: 代码有什么问题,尤其是redirectToVC方法? 任何帮助都将不胜感激。我刚刚找到了答案。它基本上是从AppDelegate显示一个视图控制器 func redirectToVC() { let mainStoryboard : UIStoryboard = UIStoryboard

我正在处理本地通知,我正在尝试展示一个特定的viewController,但我尝试了我在这个论坛中发现的内容,我在中显示的视图中发现了一个不寻常的行为 以下是AppDelegate.swift的源代码:

代码有什么问题,尤其是redirectToVC方法?
任何帮助都将不胜感激。

我刚刚找到了答案。它基本上是从AppDelegate显示一个视图控制器

func redirectToVC() {
    let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "versesVC") as UIViewController
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
}

由于

每当您必须在当前屏幕上显示特定的视图控制器时,无论您是通过导航查看控制器还是可能显示控制器,在这两种情况下,您都可以使用下面的代码来解决问题

                    var appdelgateObj = UIApplication.shared.delegate as! AppDelegate
                    if let destinationVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: “ToPresentVC”) as? ToPresentVC {
                        if let window = appdelgateObj.window , let rootViewController = window.rootViewController {
                            var currentController = rootViewController
                            while let presentedController = currentController.presentedViewController {
                                currentController = presentedController
                            }
                            currentController.present(destinationVC, animated: true, completion: nil)
                        }
                    }

不能在导航控制器上显示viewcontroller。您需要让topviewcontroller演示。以下是执行此操作的代码:-

    guard let topVC = UIApplication.getTopViewController() else {
        return
    }
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
        self.launchViewController(topVC)
    }
使用以下方法获取俯视图控制器:-

extension UIApplication {

class func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {

    if let nav = base as? UINavigationController {
        return getTopViewController(base: nav.visibleViewController)

    } else if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
        return getTopViewController(base: selected)

    } else if let presented = base?.presentedViewController {
        return getTopViewController(base: presented)
    }
    return base
  }
}
而launchViewController func是:-

class func launchViewController(_ sender: UIViewController) {
        let vc: Viewcontroller = storyboard.instantiateViewController(withIdentifier: "Viewcontroller") as! Viewcontroller
        sender.navigationController?.present(vc, animated: true, completion: nil)
    }
斯威夫特5

在Appdelegate类中创建变量:

    let window : UIWindow? = UIWindow()
并在需要重定向的位置使用以下函数:

func navigateToVCOne() {
    if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "pbVC") as? ProblemViewController {
        if let window = self.window, let rootViewController = window.rootViewController {
            var currentController = rootViewController
            while let presentedController = currentController.presentedViewController {
                currentController = presentedController
            }
            currentController.present(controller, animated: true, completion: nil)
        }
    }
}

它现在可以工作了吗?看看源代码上面的图片,视图控制器是空白的,这是不应该的。我也像这样设置了rootViewController,但问题是我默认的rootView是一个午餐动画视图。它将显示一个动画,然后重定向到另一个视图并将其设置为rootView。但是,当我将rootView重置为didReceive响应时:它会显示特定的ViewController以供浏览,然后返回到旧的rootViewController。如何防止它返回到旧的rootViewController?
func navigateToVCOne() {
    if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "pbVC") as? ProblemViewController {
        if let window = self.window, let rootViewController = window.rootViewController {
            var currentController = rootViewController
            while let presentedController = currentController.presentedViewController {
                currentController = presentedController
            }
            currentController.present(controller, animated: true, completion: nil)
        }
    }
}