【问题标题】:iOS 9 selecting tabbar index with quick actions, performActionForShortcutItemiOS 9 选择带有快速操作的标签栏索引,performActionForShortcutItem
【发布时间】:2015-11-07 22:44:15
【问题描述】:

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

【问题讨论】:

    标签: ios swift ios9 3dtouch


    【解决方案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-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 2016-06-25
      相关资源
      最近更新 更多