Swift:如何通过快速操作访问AppDelegate中的某个TabBar选项卡?
问题描述:
我正在为应用程序快速采取行动。它工作正常。因此,在AppDelegate中我检查了shortcutItem这样并调用completionHandler功能:Swift:如何通过快速操作访问AppDelegate中的某个TabBar选项卡?
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(并显示它)通过快速行动中的另一个选项卡?
答
我得出了一个解决方案,但我不知道这是否是做到这一点的最好办法:
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()
}