2010-08-09 57 views
8

我想在用戶單擊另一個選項卡時立即移除徽章。我正在嘗試:如何在用戶單擊另一個選項卡後刪除UITabBar徽章?

- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 

    UITabBarItem *tbi = (UITabBarItem *)self.tabController.selectedViewController.tabBarItem; 
    tbi.badgeValue = nil; 
} 

但它不起作用。

+0

可能重複http://stackoverflow.com/questions/2476903/remove-bagde-from-tabbar-item – jer 2010-08-10 00:03:42

回答

10

您想從當前選項卡中移除一個徽章,還是要移除該徽章?

這樣做的正確的地方,無論哪種方式,是在你的標籤欄控制器的委託,於:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController; 

注意,只要用戶點擊標籤欄按鈕,無論這個函數被調用顯示的新視圖控制器與舊視圖控制器不同,因此您需要跟蹤當前可見視圖控制器。在這裏,你會更新,也:

- (void)tabBarController:(UITabBarController *)tabBarController 
     didSelectViewController:(UIViewController *)viewController { 
    if(viewController != self.currentTabVC) { 
     // if you want to remove the badge from the current tab 
     self.currentTabVC.tabBarItem.badgeValue = nil; 

     // or from the new tab 
     viewController.tabBarItem.badgeValue = nil; 

     // update our tab-tracking 
     self.currentTabVC = viewController; 
    } 
} 
+0

順便說一句,我不知道如果不加思索-tabBarController:didSelectViewController:在創建標籤欄時被調用,或者您需要在-applicationDidFinishLoading中手動設置self.currentTabVC。 – 2010-08-10 00:22:28

相關問題