0

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

4

1 回答 1

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方法中实现逻辑

正如苹果文档所述,如果您想更改首页,它是在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

于 2015-12-18T05:26:18.397 回答