如何从应用程序委托中打开视图控制器?

问题描述 投票:0回答:1

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

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

        completionHandler()

    }

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

希望您能帮助我。谢谢!

ios swift xcode appdelegate rootview
1个回答
0
投票

您有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? UITabController {
              tabController.selectedIndex = index
        }

    }
© www.soinside.com 2019 - 2024. All rights reserved.