iOS 9选择带快速操作的tabbar索引,performActionForShortcutItem

时间:2015-11-07 22:44:15

标签: ios swift ios9 3dtouch

我有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,然后调用此方法:

1 个答案:

答案 0 :(得分:1)

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

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

如果您有5个标签,并且想要快速操作另一个标签页, 您应该动态更改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 方法中实现逻辑

正如Apple提到的那样,如果您想要更改第一页,它是 didFinishLaunchingWithOptions 中快速操作逻辑的最佳位置。 在我的情况下, performActionForShortcutItem 仅被调用来处理应用程序的背景。(你应该在 didFinishLaunchingWithOptions 中返回NO以处理 didFinishLaunchingWithOptions 中的快速操作)< / p>

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