0

当从通知中心打开本地通知时,我想打开“菜单视图控制器”。我的问题是我不知道如何从 AppDelegate 打开视图控制器。我发现的所有解决方案都不起作用,因为它适用于在 SceneDelegate 引入之前的旧 Xcode 版本。这是我要调用视图控制器的 AppDelegate 中的函数。

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        // missing code to open menu view controller

        completionHandler()

    }

我使用标签栏控制器在一些视图控制器之间导航。我不确定这是否会有所作为。这是我的故事板的重要部分的外观。 袋鼠

我希望你能帮助我。谢谢!

4

1 回答 1

1

您有 2 种可能的方法来执行此操作:

1. 通过情节提要实例化您的标签栏控制器。

转到您的情节提要并在身份检查器中为您的标签栏控制器设置情节提要 ID。

一旦你这样做了,你可以像这样实例化你的视图控制器:

let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
if let tabController = storyboard.instantiateViewController(withIdentifier: "tabControllerID") as? UITabController {
    tabController.selectedIndex = index
}
  1. 或者,您可以从 AppDelegate 访问场景委托并使用 window -> rootViewController 来访问您的 tabController:
    let scene = UIApplication.shared.connectedScenes.first
    if let sceneDelegate = scene?.delegate as? SceneDelegate {  
        if let tabController = sceneDelegate.window?.rootViewController as? UITabBarController {
              tabController.selectedIndex = index
        }

    }
于 2020-06-01T23:40:43.600 回答