【问题标题】:Badge value on "More" tab“更多”选项卡上的徽章值
【发布时间】:2011-09-25 02:51:30
【问题描述】:

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

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

我从question 知道如何做到这一点。

但我的标签顺序是可配置的。有没有办法可以配置 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 也应相应更新。

谢谢。

【问题讨论】:

    标签: iphone objective-c uitabbar badge


    【解决方案1】:

    试试这个:

    - (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]];
        }
    }
    

    我认为它会起作用。

    【讨论】:

    • 谢谢卡希夫。这只是关于“添加”到可用计数。我也在寻找标签顺序更改时的更新。在侧节点上,正确的语句应该是 .. moreTabCount = [... badgeValue] intValue];,因为 badgeValue 是一个 NSString。
    • 我想我只需要这样做。似乎没有任何 API 可以自动处理这些内容。我的意思是 MORE 选项卡是由系统本身创建的,所以它也应该管理这个选项卡:P。
    【解决方案2】:

    试试这个

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

    这里ObjectAtIndex 用于您的选项卡,其中 0 代表您的第一个选项卡等...

    【讨论】:

    • 请阅读完整的帖子。我知道如何将徽章值放在更多选项卡上。我想知道让它与我的标签同步。
    【解决方案3】:

    您可以用于 Swift 4:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-06
      • 2015-11-17
      • 1970-01-01
      • 2013-02-17
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多