iOS 9 selecting tabbar index with quick actions, p

2019-09-02 10:20发布

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条回答
戒情不戒烟
2楼-- · 2019-09-02 11:02

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

查看更多
登录 后发表回答