iOS 9 selecting tabbar index with quick actions, performActionForShortcutItem

此生再无相见时 提交于 2019-12-12 06:06:37

问题


I have 5 tabs and want to go to a specific tab when user select a certain Quick Action.

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 is parent, tabVC is child of revealVC, then navVC is child of tab.

Again, I've tried using notificationCenter and referencing the shopVC, then calling this method:


回答1:


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) create SWRevealVC programmatically(not in the storyboard)

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) implement logic in the didFinishLaunchingWithOptions method

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];
    }

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



来源:https://stackoverflow.com/questions/33588676/ios-9-selecting-tabbar-index-with-quick-actions-performactionforshortcutitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!