我正在为一个应用设置快速操作。它工作正常。所以在 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/