我有 5 个选项卡,当用户选择某个快速操作时,我想转到一个特定的选项卡。
但是,我尝试过使用通知中心,引用主视图控制器并引用应用程序委托中的选项卡,但似乎都不起作用。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 是tab 的子级。
同样,我尝试使用 notificationCenter 并引用 shopVC,然后调用此方法: