0

我的 tabBar 项目似乎有问题。创建 tabBar 我可以设置我想要的 tabBarItem 的这个属性,如下所示:

viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    UIViewController *viewController3 = [[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil];


    UIViewController *viewController4 = [[FourthViewController alloc]initWithNibName:@"FourthViewController" bundle:nil];

    //Create our NavigationViewController object
    NavigationViewController *navController = [[NavigationViewController alloc] initWithNibName:@"NavigationViewController" bundle:nil];

    //Create our UINavigationController
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:navController];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[nav, viewController2, viewController4, viewController3];

    nav.tabBarItem = [[UITabBarItem alloc]
                      initWithTitle:(@"Menukort")
                      image:[UIImage imageNamed:@"162-receipt.png"]
                      tag:0];

    viewController2.tabBarItem = [[UITabBarItem alloc]
                                  initWithTitle:(@"Favoritter")
                                  image:[UIImage imageNamed:@"28-star.png"]
                                  tag:1];

    viewController3.tabBarItem = [[UITabBarItem alloc]
                                  initWithTitle:(@"Info")
                                  image:[UIImage imageNamed:@"104-index-cards.png"]
                                  tag:2];

    viewController4.tabBarItem = [[UITabBarItem alloc]
                                  initWithTitle:(@"Kort")
                                  image:[UIImage imageNamed:@"103-map.png"]
                                  tag:3];

    viewController2.tabBarItem.badgeValue = @"1";    

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];    

所有这些代码都在一个单例中,因此共享对象应该不是问题。现在 - 我将如何在另一个类中设置这个 tabBarItem 的 badgeValue?

提前致谢。

4

2 回答 2

0

我自己在另一个线程中找到了答案。

我是这样做的:

[[[[[self tabBarController] tabBar] items] 
                   objectAtIndex:tabIndex] setBadgeValue:badgeValueString];
于 2012-12-07T07:52:14.730 回答
0

使用获取 tabbarController 的 viewController 数组

NSArray *arr = [self.tabbarController viewController];

然后使用任何视图控制器设置值

[[[arr objectIndex:1] tabBarItem] setbadgeValue:<nsinteger some value>];
于 2012-12-06T19:08:16.277 回答