【问题标题】:Reload badge of tab bar item重新加载标签栏项目的徽章
【发布时间】:2013-05-23 10:16:35
【问题描述】:

我有一个带有标签栏控制器的应用程序。这些视图之一是表格视图。有一种方法可以在标签栏中设置此视图的徽章。这有效......但只有当用户触摸此视图而不是在启动应用程序时才正确。所以我尝试在 appDelegate 中使用这个方法……但这不起作用。 我在视图中的方法:

    @property (strong) NSMutableArray *cars;
//some code here

    -(void)SelectBadge
    {
        int r = [_cars count];
        if (r == 0) {
            self.navigationController.tabBarItem.badgeValue = 0;
        }
        else {
        self.navigationController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", r];
        }
        [self.tableView reloadData];
    }

我尝试将此方法放入我的 appDelegate 文件中:

- (void)applicationDidBecomeActive:(UIApplication *)application
{    
    CarList *Instance = [[CarList alloc] init];
    [Instance SelectBadge];
}

提前感谢您的所有回答。

【问题讨论】:

    标签: ios tabbar appdelegate badge


    【解决方案1】:

    我看到的方式是您正在此 - (void)applicationDidBecomeActive:(UIApplication *)application method. 中创建 CarList 的新实例,因此在 selectBadge 函数中,self.navigationController.tabBarItem.badgeValue = someValue; 将为其他实例设置徽章值。

    尝试寻址正确的实例。如果您可以访问 UITabBarController 实例,那么您可以这样做:

    UITabBar     *tabBar = mTabBarController.tabBar;
    UITabBarItem *someItem  = [tabBar.items objectAtIndex:0];////You can put your interested tabBarItem index
    someItem. badgeValue = @"100";
    

    【讨论】:

    • 它是一个 UITabBarController 实例。正如您所说,您有一个带有标签栏控制器的应用程序。我指的是那个标签栏控制器
    【解决方案2】:

    假设您的 ViewControllers 是从 StoryBoard 加载的,请调用您的函数来更新您要更新其 tabBarItem badgeValue 的 ViewController 的“initWithCoder:”中的 tabBarItem badgeValue。与 TabBarController 中的选项卡关联的 ViewController 在 TabBar 加载时被初始化。

    代码可能如下所示:

        - (id) initWithCoder:(NSCoder *)aDecoder
        {    
              self = [super initWithCoder:aDecoder];
    
              NSString* badgeValue = [self calculateBadgeValue];  //your method
              self.tabBarItem.badgeValue = badgeValue;
    
              return self;
         }
    

    如果你这样做,当 TabBar 变得可见时,徽章应该会更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-22
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多