【问题标题】:Swift: How to access a certain TabBar tab from AppDelegate with Quick Actions?Swift:如何通过快速操作从 AppDelegate 访问某个 TabBar 选项卡?
【发布时间】:2017-09-22 19:22:51
【问题描述】:

我正在为应用设置快速操作。它工作正常。所以在 AppDelegate 中,我正在检查这样的快捷方式项目并调用 completionHandler 函数:

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {

    completionHandler(handleShortcut(shortcutItem: shortcutItem))
}

现在我想在 handleShortcut 函数中打开某个选项卡,但这根本不起作用。我尝试将其加载为带有 storyboardID 的视图并将其添加为 rootViewController,但我什至不确定这是否可行,或者这是否是正确的方法:

    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let vc = storyBoard.instantiateViewController(withIdentifier: "tabBar")
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = vc
    self.window?.makeKeyAndVisible()

但显然我不能使用tabBarController?.selectedIndex = 2 来访问第三个标签。

通过快速操作访问我的 tabBarController 中的另一个选项卡(并显示它)的正确方法是什么?

【问题讨论】:

    标签: ios swift uitabbarcontroller appdelegate quickaction


    【解决方案1】:

    我已经找到了解决方案,但我不确定这是否是最好的方法:

    if shortcutItem.type == "com.xxx.openHelp" {
                let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
                let vc = storyBoard.instantiateViewController(withIdentifier: "tabBar")
                self.window = UIWindow(frame: UIScreen.main.bounds)
                self.window?.rootViewController = vc
                let myTabBar = self.window?.rootViewController as! UITabBarController
                myTabBar.selectedIndex = 3
                self.window?.makeKeyAndVisible()
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多