2017-09-22 75 views
0

我正在爲應用程序快速採取行動。它工作正常。因此,在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(並顯示它)通過快速行動中的另一個選項卡?

回答

0

我得出了一個解決方案,但我不知道這是否是做到這一點的最好辦法:

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() 
     } 
相關問題