【问题标题】:How do I update the badge number for uitabbaritem when a new push notification arrives当新的推送通知到达时,如何更新 uitabbaritem 的徽章编号
【发布时间】:2015-05-15 07:40:53
【问题描述】:

当新的推送通知到达时,如何更新特定 UITabBarItem 的徽章? 例如像 Instagram 中的活动标签

【问题讨论】:

    标签: ios swift uitabbar uitabbaritem


    【解决方案1】:

    您应该使用以下代码:

    [[[[[self tabBarController] tabBar] items] 
                   objectAtIndex:tabIndex] setBadgeValue:badgeValueString];
    

    【讨论】:

    • 是的,但是如何从处理推送通知的 AppDelegate 访问 tabbarController?
    • 你在哪里创建了tabbarcontroller?我猜,应该是在 AppDelegate
    • 我只是把它拖到情节提要上
    • 请检查:self.window.rootViewController。是tabbarcontroller实例吗?
    • rootViewController 是一个 UIView。当我尝试访问 rootViewController.tabBarController 我得到 nil
    【解决方案2】:
    NSString *badgeValue = [NSString stringWithFormat:@"%lu", (long)countUnreadMessages];
    
    [[[[self viewControllers] objectAtIndex:indexOfTabbarItem] tabBarItem] setBadgeValue:([badgeValue isEqualToString:@"0"]) ? nil : badgeValue];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      • 2011-09-19
      • 2014-05-06
      • 2012-04-02
      • 2014-07-18
      相关资源
      最近更新 更多