通知操作

时间:2017-02-09 14:35:48

标签: swift viewcontroller appdelegate unusernotificationcenter usernotifications

我正在处理本地通知,并且我试图提出具体的viewController,但我已经尝试了我在此论坛中发现的内容,并且我在视图显示在 this picture here: 这里是 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 个答案:

答案 0 :(得分:2)

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

感谢Opening view controller from app delegate using swift

答案 1 :(得分:1)

每当你必须在当前屏幕上呈现一个特定的视图控制器时,无论你是通过导航推送的视图控制器还是可能被呈现,都可以使用下面的代码来解决你的问题。

for %%G in (*.sql) do sqlcmd /S W-VAN-A124178\HAZUSPLUSSRVR /d 
BC_Exposure_2016 -E -i"%%G"
pause

答案 2 :(得分:0)

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

    guard let topVC = UIApplication.getTopViewController() else {
        return
    }
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
        self.launchViewController(topVC)
    }

使用以下方法获取Top View控制器:-

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函数是:-

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

答案 3 :(得分:0)

在Swift 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)
        }
    }
}