问题描述
我有5个标签,当用户选择某个快速操作时,我想转到一个特定的标签.
I have 5 tabs and want to go to a specific tab when user select a certain Quick Action.
但是,我尝试使用通知中心,引用主viewcontroller并引用应用程序委托中的选项卡,但似乎没有任何作用.确实会调用tabbar.selectedIndex方法,但是由于某些原因,使用快速操作时选项卡没有更改.
However, I've tried using notification center, referencing the master viewcontroller and referencing the tab in app delegate but none seems to work. The tabbar.selectedIndex method does get called but for some reasons the tab isn't changing when using quick action.
@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的子级.
revealVC is parent, tabVC is child of revealVC, then navVC is child of tab.
同样,我尝试使用NotificationCenter并引用shopVC,然后调用此方法:
Again, I've tried using notificationCenter and referencing the shopVC, then calling this method:
推荐答案
最近,我已经使用SWRevealVC库实现了主屏幕快速操作.因此,很高兴告诉您如何解决此问题.
Recently, I have implemented home screen quick action with SWRevealVC library. So, I'm very glad to tell you how to solve this issue.
1)以编程方式创建SWRevealVC(不在情节提要中)
1) create SWRevealVC programmatically(not in the storyboard)
如果您有5个标签,并且想通过快速操作来移动另一个标签,您应该动态更改SWRevealVC的frontVC以响应快速操作.
If you have 5 tabs and you want to move another tab with quick action,you should change frontVC of SWRevealVC dynamically to response to quick action.
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 方法中实现逻辑
2) implement logic in the didFinishLaunchingWithOptions method
如苹果文件所述,如果要更改首页,它是 didFinishLaunchingWithOptions 中执行快速操作逻辑的最佳位置.在我的案例中,仅调用 performActionForShortcutItem 处理应用程序的背景.(您应在 didFinishLaunchingWithOptions 中返回NO,以处理 didFinishLaunchingWithOptions 中的快速操作)
As apple documented mentioned, it is the best place for quick action logic in didFinishLaunchingWithOptions if you want to change first page.In my case, performActionForShortcutItem is only called to deal with app's background.(you should return NO in didFinishLaunchingWithOptions to handle quick action in 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).我希望它能解决您的问题.
And I give a sample project what I make(objective-c) for you. I hope it will solve your problem.
https://github.com/dakeshi/3D_Touch_HomeQuickAction
这篇关于iOS 9通过快速操作选择tabbar索引,performActionForShortcutItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!