2

我正在制作一个 iOS 应用程序,它有一个 tabBarController,有超过 5 个选项卡。因此,前四个是可直接点击的,其余的都在 MORE 选项卡下。

如果此 MORE 选项卡中隐藏的选项卡有任何标记,我想在 MORE 选项卡上显示标记。

我知道如何从这个问题中做到这一点。

但是我的标签的顺序是可配置的。有没有办法可以配置 MORE 选项卡,如果我在其中设置选项卡的值,它只会放置 badgeValue?

我在想这个:

- (void)updateBadgeValue:(NSInteger)count {
    int index = [self.tabBarController indexOfObject:self.tabBarItem];
    if (index > 4) { //Inside MORE tab
        [[[self.tabBarController moreTabBarController] tabBarItem] setBadgeValue:[NSString stringWithFormat:@"%d", count]];
    }
    //Also setting badge of self.tabbarItem so that it remains when it is brought to "hot tab items".
}

我正在寻找一种解决方案,这样我就不必为每个标签都这样做。此外,如果用户更改了 tab 顺序,badgeValue 也应该相应地更新。

谢谢。

4

3 回答 3

4

尝试使用这个:

- (void)updateBadgeValue:(NSInteger)count {
    int index = [self.tabBarController indexOfObject:self.tabBarItem];
    if (index > 4) {
        int moreTabCount = count + [[[[self.tabBarController moreTabBarController] tabBarItem] badgeValue] intValue];
        [[[self.tabBarController moreTabBarController] tabBarItem] setBadgeValue:[NSString stringWithFormat:@"%d", moreTabCount]];
    }
}

更新:您可以通过使用响应配置更改

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed

您的委托中的委托方法UITabBarController(应该是AppDelegate)。我们开始做吧:

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
    if(changed) {
        int count = 0;
        int i; for(i = 4; i < [viewControllers count]; i++)
            count += [[[[viewControllers objectAtIndex:i] tabBarItem] badgeValue] intValue];
        if(count > 0)
            [[self.tabBarController moreTabBarController] tabBarItem] setBadgeValue:[NSString stringWithFormat:@"%d", count]];
    }
}

我认为它会起作用。

于 2011-06-29T13:40:42.753 回答
1

尝试使用这个

[[[[[self tabBarController] tabBar] items] 
          objectAtIndex:4] setBadgeValue:[NSString stringWithFormat:@"%d",yourBadgeValue];

ObjectAtIndex是您的选项卡,其中 0 代表您的第一个选项卡等...

于 2011-06-29T12:22:40.717 回答
0

您可以用于 Swift 4:

让 messagesCount: Int = 5 self.tabBarController?.moreNavigationController.tabBarItem.badgeValue = " \(messagesCount)"

于 2018-01-01T19:58:49.993 回答