2012-05-01 57 views
5

我爲項目實現了自定義UITabBar解決方案。從本質上講,如果有超過5個項目,我使用scrollView,將允許用戶滾動其他標籤項目,並抑制更多的按鈕。在Weather Channel應用程序中可以看到類似的外觀和感覺。抑制自定義UITabBarController中的更多導航控制器

每個標籤欄項目對應一個管理每個標籤的視圖堆棧的UINavigationController。我遇到的問題是當我有超過5個選項卡項目時,從選項卡5開始並未正確維護導航堆棧。似乎moreNavigationController每次返回到該選項卡時都會導致導航堆棧死機,並且您將再次進入初始頁面。

我已經覆蓋了setSelectedViewController方法如下:

- (void) setSelectedViewController:(UIViewController *)selectedViewController { 
    [super setSelectedViewController:selectedViewController]; 
    if ([self.moreNavigationController.viewControllers count] > 1) { 
     self.moreNavigationController.viewControllers = [[NSArray alloc] initWithObjects:self.moreNavigationController.visibleViewController, nil]; 
    } 
} 

該代碼將刪除左側導航欄按鈕更多功能,但它並沒有解決保持導航堆棧的問題。所有其他標籤工作正常。我可以遍歷幾個視圖,並在離開並返回到該選項卡後維護堆棧。我明白這是一個複雜的問題,所以請讓我知道,如果有一些地方我可以提供清晰。謝謝!

回答

5

這是我是如何結束這個固定:

- (void) setSelectedViewController:(UIViewController *) selectedViewController { 
    self.viewControllers = [NSArray arrayWithObject:selectedViewController]; 
    [super setSelectedViewController:selectedViewController]; 
} 

基本上從5獲取由moreNavigationController取代它的導航控制器,當你intiially設置的UITabBarController的viewControllers任何標籤。因此,我動態設置viewControllers只包含我點擊的選項卡。在這種情況下永遠不會超過1,所以更多導航控制器不會發揮作用。

當我啓動我的自定義控制器時,我只提供第一個選項卡作爲viewControllers,以便應用程序可以加載。

- (id) init { 
    self = [super init]; 
    if (self) { 
     self.delegate = self; 
     [self populateTabs]; 
    } 
    return self; 
} 

- (void) populateTabs { 
    NSArray *viewControllers = [self.manager createViewsForApplication]; 
    self.viewControllers = [NSArray arrayWithObject:[viewControllers objectAtIndex:0]]; 
    self.tabBar.hidden = YES; 
    MyScrollingTabBar *tabBar = [[MyScrollingTabBar alloc] initWithViews:viewControllers]; 
    tabBar.delegate = self; 
    [self.view addSubview:tabBar]; 
} 

爲了清楚起見,使用TabBar委託設置爲該類以便它可以以標籤響應點擊。代表方法如下:

- (void) tabBar:(id) bar clickedTab:(MyScrollingTabBarItem *) tab { 
     if (self.selectedViewController == tab.associatedViewController) { 
     [(UINavigationController *) tab.associatedViewController popToRootViewControllerAnimated:YES]; 
    } else { 
     self.selectedViewController = tab.associatedViewController; 
    } 
    // keep nav label consistent for tab 
    self.navigationController.title = tab.label.text; 
} 
+1

我做了相當不同的事情,但是使用了您的概念,只放置第一個ViewController並動態加載所有其他的。做得很好! – Marquis103

相關問題