iOS 9使用快速操作selecttabbar索引,performActionForShortcutItem

我有5个选项卡,并希望在用户select某个快速操作时转到特定选项卡。

不过,我试过使用通知中心,引用主视图控制器和引用应用程序委托中的选项卡,但似乎没有工作。 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) } 

显示VC是父项,tabVC是显示的子项,然后navVC是项的子项。

我再次尝试使用notificationCenter并引用shopVC,然后调用这个方法:

最近,我用SWRevealVC库实现了主屏幕快速操作。 所以,我很高兴告诉你如何解决这个问题。

1)以编程方式创buildSWRevealVC(不在故事板中)

如果您有5个选项卡,并且想要快速移动另一个选项卡,则应该dynamic更改SWRevealVC的frontVC以响应快速操作。

 UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window = window; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UINavigationController *frontViewController = [storyboard instantiateViewControllerWithIdentifier:@"mainNaviVC"]; SideMenuViewController *rearViewController = [storyboard instantiateViewControllerWithIdentifier:@"sideMenuVC"]; SWRevealViewController *mainRevealController = [[SWRevealViewController alloc] initWithRearViewController:rearViewController frontViewController:frontViewController]; self.window.rootViewController = mainRevealController; 

2)在didFinishLaunchingWithOptions方法中实现逻辑

如提到的苹果文档,如果您想更改第一页,则它是didFinishLaunchingWithOptions中快速操作逻辑的最佳位置。 在我的例子中,只调用performActionForShortcutItem来处理应用程序的背景(在didFinishLaunchingWithOptions中你应该返回NO来处理didFinishLaunchingWithOptions中的快速操作)

 if ([shortcutItem.type isEqualToString:shortcutAroundStop]) { handled = YES; UINavigationController *frontViewController = [storyboard instantiateViewControllerWithIdentifier:@"naviStopAroundVC"]; SideMenuViewController *rearViewController = [storyboard instantiateViewControllerWithIdentifier:@"sideMenuVC"]; SWRevealViewController *mainRevealController = [[SWRevealViewController alloc] initWithRearViewController:rearViewController frontViewController:frontViewController]; self.window.rootViewController = mainRevealController; [self.window makeKeyAndVisible]; } 

我给你一个样本项目,我为你做了什么(objective-c)。 我希望它能解决你的问题。

https://github.com/dakeshi/3D_Touch_HomeQuickAction