ios - Swift:如何使用快速操作从 AppDelegate 访问某个 TabBar 选项卡?

标签 ios swift uitabbarcontroller appdelegate quickaction

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

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

    completionHandler(handleShortcut(shortcutItem: shortcutItem))
}

现在我想在 handleShortcut 函数中打开某个选项卡,但这根本不起作用。我尝试使用它的 Storyboard ID 将它加载为 View 并将其添加为 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 中的另一个选项卡(并显示它)的正确方法是什么?

最佳答案

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

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()
        }

关于ios - Swift:如何使用快速操作从 AppDelegate 访问某个 TabBar 选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46371864/

相关文章:

ios - 激活 segue 时自动布局无法满足约束

swift - 覆盖 isReachable 不适用于模拟

ios - 如果一个变异函数设置它自己等于另一个函数,这意味着什么

ios - Json 数据和我所做的类不匹配

ios - UITextView 放置在自动布局中表现得很奇怪

ios - 数组在函数中填充并显示但在 tableview swift 中不显示

ios - SVG 填充图像源的回退

iphone - UITabBarController 与 viewControllers 利用不同的方向?

ios - 在多个 View Controller 中共享一个 UIPicker(Tab bar Controller tabs)

c# - 如何从在 Xamarin IOS 中添加 UITabBarController 的另一个 Controller 更改标签的文本值