2015-11-07 78 views
0

我有5個選項卡,並且希望在用戶選擇某個快速操作時轉到特定選項卡。iOS 9使用快速操作選擇tabbar索引,performActionForShortcutItem

但是,我試過使用通知中心,引用主視圖控制器和引用應用程序委託中的選項卡,但似乎沒有工作。 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是選項卡的子項。

再次,我已經使用通知中心,並引用shopVC,則調用此方法的嘗試:

回答

1

最近,我已經實現了主屏幕,SWRevealVC庫迅速採取行動。所以,我很高興告訴你如何解決這個問題。

1)創建SWRevealVC編程方式(而不是在故事板)

如果你有5個選項卡,並要與快速移動的動作的另一個選項卡, 你應該動態地改變SWRevealVC的frontVC以應對迅速採取行動。在didFinishLaunchingWithOptions

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如果你想改變第一頁的最佳場所。 在我的情況下,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