我有5個選項卡,並且希望在用戶選擇某個快速操作時轉到特定選項卡。iOS 9使用快速操作選擇tabbar索引,performActionForShortcutItem
但是,我試過使用通知中心,引用主視圖控制器和引用應用程序委託中的選項卡,但似乎沒有工作。 tabbar.selectedIndex方法確實被調用,但由於某些原因,使用快速操作時該選項卡未更改。
@available(iOS 9.0, *)
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
let revealVC = self.window!.rootViewController as! SWRevealViewController
let tabVC = revealVC.childViewControllers[1] as! UITabBarController
let navVC = tabVC.viewControllers![0] as! UINavigationController
let shopVC = navVC.viewControllers[0] as! BrowseVC
switch shortcutItem.type {
case "com.modesens.search" :
tabVC.selectedIndex = 0
//referencing method to go to tab in base view controller also doesn't work...
//shopVC.goToSelectTab (0)
completionHandler(true)
//notification center method gets called but tab actually doesn't change
//NSNotificationCenter.defaultCenter().postNotificationName("goToTab", object: nil, userInfo: ["tabIndex":0])
default:
print("no work")
}
completionHandler(false)
}
revealVC是父項,tabVC是revealVC的子項,則navVC是選項卡的子項。
再次,我已經使用通知中心,並引用shopVC,則調用此方法的嘗試: