1

我有一个带有标签栏控制器的应用程序。这些视图之一是表格视图。有一种方法可以在标签栏中设置此视图的徽章。这有效...但仅当用户触摸此视图而不是在启动应用程序时才正确。所以我尝试在 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];
}

事先感谢您的所有回答。

4

2 回答 2

1

我看到的方式是您正在此中创建 CarList 的新实例,- (void)applicationDidBecomeActive:(UIApplication *)application method.因此在 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";
于 2013-05-23T10:32:59.540 回答
0

假设您的 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 变得可见时,徽章应该会更新。

于 2014-05-22T05:35:35.657 回答