2

我正在使用本地通知,我正在尝试提供一个特定的通知,viewController但我已经尝试了我在这个论坛中找到的内容,我得到了一个不寻常的行为, 这里的图片显示了这个视图:这是AppDelegate 的源代码.swift

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    print("didReceive Method called")

    if response.actionIdentifier == "actionOne" {
        DispatchQueue.main.async(execute: {
            self.notificationAction1()
        })
    } else if response.actionIdentifier == "actionTwo" {
        DispatchQueue.main.async(execute: { 
            self.notificationAction2()
        })

    } else if response.actionIdentifier == "actionThree" {

    }
    completionHandler()
}

func notificationAction1() {
    redirectToVC()
}

func redirectToVC() {
    let toVC = VersesViewController()
    if self.window != nil && self.window?.rootViewController != nil {
        let rootVC = self.window?.rootViewController!
        if rootVC is UINavigationController {
            (rootVC as! UINavigationController).pushViewController(toVC, animated: true)
        } else {
            rootVC?.present(toVC, animated: true, completion: { 
                //Do something
            })
        }
    }
}

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

4

4 回答 4

3

我刚刚找到了答案。它基本上是从 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()
}

感谢使用 swift 从应用程序委托打开视图控制器

于 2017-02-10T06:39:02.573 回答
1

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

                    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)
                        }
                    }
于 2017-09-19T05:53:28.040 回答
0

您不能在导航控制器上显示视图控制器。您需要让 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)
    }
于 2020-06-12T03:35:53.113 回答
0

在斯威夫特 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)
        }
    }
}
于 2020-06-12T05:00:45.580 回答