【问题标题】:BadgeValue not getting updated on UITabBarItemBadgeValue 未在 UITabBarItem 上更新
【发布时间】:2013-05-06 10:49:58
【问题描述】:

我使用UITabBarUITabBarItems。在将tabBarItem 分配给tabBar 之前,我可以为UITabBarItem 设置徽章值。但我的问题是我无法更新tabBarItem 的徽章值。

这是我最初可以设置徽章值的代码:

// array of tabBarItems
NSMutableArray * tabs = [[NSMutableArray  alloc] init];

for(iterates few times)
{
    [tabs addObject:[[UITabBarItem alloc] initWithTitle:firstName image:nil tag:i]];
    // set tabItem's property
    [(UITabBarItem *)[tabs objectAtIndex:i] setFinishedSelectedImage:[self convertImage:iconImage toSize:CGSizeMake(iconWidth, TAB_ICON_HEIGHT)] withFinishedUnselectedImage:[self convertImage:iconImage toSize:CGSizeMake(iconWidth, TAB_ICON_HEIGHT)]];
    [[tabs objectAtIndex:0] setBadgeValue:[NSString stringWithFormat:@"%d", 2]];
}
// setting items of UITabBar
[self.chatTabBar setItems:tabs];

在这里,我尝试更新徽章值。所以发生的情况是,如果我 NSLog 新的徽章值,它会显示更新后的值,但我看到 UI 没有变化。

[[self.chatTabBar.items objectAtIndex:0] setBadgeValue:[NSString stringWithFormat:@"%d", 1]];

【问题讨论】:

    标签: ios objective-c ios6 uitabbar uitabbaritem


    【解决方案1】:

    试试看:

    UIViewController *carrinhoVC = [self.tabBarController.viewControllers objectAtIndex:0];
    
                carrinhoVC.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", 1];
    

    carrinhoVC 是您要在 TabBar 中更新徽章值的 UIViewController。

    【讨论】:

    • 5 分钟前你说它有效,再次检查,你做错了什么,这段代码运行良好。
    • 如果是我的代码错误,我会接受你的回答。但要有耐心。
    【解决方案2】:

    这段代码对我有用:

    if let tabBar = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController,
        let tabBarItem = tabBar.tabBar.items?[1] {
        tabBarItem.badgeValue = nil
    }
    

    【讨论】:

    • 这是“rockdaswift”唯一有效的方法
    猜你喜欢
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    相关资源
    最近更新 更多