【问题标题】:Auto-update UITabBar item badge value自动更新 UITabBar 项目徽章值
【发布时间】:2014-08-25 12:09:05
【问题描述】:

在我的应用程序中有 UITabBar 项目,我希望标签栏项目的徽章值每 X 秒更新一次,但我无法弄清楚...

这是我的更新方法:

-(void)updateTabBadgeValue{

    NSLog(@"tick");


    if([PFUser currentUser]!=nil){
        NSLog(@"user is not null");
        UIStoryboard *mySb = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];

        UITabBarController *myTbc = [mySb instantiateViewControllerWithIdentifier:@"tbc"];
        NotificationNavigation *nn = [myTbc viewControllers][2];

        NotificationViewController *nvc = [nn viewControllers][0];
         [nvc awakeFromNib];
       PFQuery *query = [PFQuery queryWithClassName:@"NoCo"];
        [query whereKey:@"username" equalTo:[[PFUser currentUser]username]];
        PFObject *noco = [query getFirstObject];
        if([[noco objectForKey:@"count"] intValue] > 0){
            NSLog(@"tock");
            [nn.tabBarItem setBadgeValue:[NSString stringWithFormat:@"%d",[[noco objectForKey:@"count"] intValue]]];
            [myTbc viewDidLoad];
            [myTbc viewWillAppear:YES];
            [nvc viewDidLoad];
            [nvc viewWillAppear:YES];

        }


    }
}

我这样实现这个方法:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
   [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateTabBadgeValue) userInfo:nil repeats:YES];

       }

但它似乎不起作用...我的日志输出显示“tick”、“user is not null”、“tock”,每 5 秒按此顺序显示,所以我知道正在调用该方法,但是徽章值未更新

【问题讨论】:

  • 试试这个,[[[[[self tabBarController] tabBar] items] objectAtIndex:tabIndex] setBadgeValue:badgeValueString];希望这会有所帮助。

标签: ios iphone uitabbaritem


【解决方案1】:

调用您的函数来更新您要更新其 tabBarItem badgeValue 的 ViewController 的 'initWithCoder:' 中的 tabBarItem badgeValue。与 TabBarController 中的选项卡关联的 ViewController 在 TabBar 加载时被初始化。我不知道你为什么打电话给viewDidLoadviewWillAppear 两次。大多数徽章由NSNotificationCenterAPNS 更新。

for (UIViewController *viewController in myTbc.viewControllers) {
   if (viewController.tabBarItem.tag == MyTabBarItemTag) 
// give tag of ur tabbar item for which you want to update badge Or you can also use index of item here.

{
    viewController.tabBarItem.badgeValue =@"1" //[NSString stringWithFormat:@"%d",[[noco objectForKey:@"count"] intValue]];
 }
}

【讨论】:

  • 视图控制器实际上是一个 UINavigationController...这样还可以吗?
  • 是的。因为 navigationcontroller 也是 tabcontroller 的子类
【解决方案2】:

最终,最终的工作是将 NSTimer 与此方法一起使用:

-(void)updateTabBadgeValue{

    NSLog(@"tick");


    if([PFUser currentUser]!=nil){
        NSLog(@"user is not null");
        //UIStoryboard *mySb = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];

        //UITabBarController *myTbc = [mySb instantiateViewControllerWithIdentifier:@"tbc"];
        //NotificationNavigation *nn = [myTbc viewControllers][2];

        //NotificationViewController *nvc = [nn viewControllers][0];
        //[nvc awakeFromNib];
        PFQuery *query = [PFQuery queryWithClassName:@"NoCo"];
        [query whereKey:@"username" equalTo:[[PFUser currentUser]username]];
        PFObject *noco = [query getFirstObject];
        if([[noco objectForKey:@"count"] intValue] > 0){
            NSLog(@"tock");
            //for(UIViewController *controller in myTbc.viewControllers){
                //if(controller.tabBarItem.tag == 101){
                    [self.tabBarItem setBadgeValue:[NSString stringWithFormat:@"%d",[[noco objectForKey:@"count"] intValue]]];

                    //[myTbc viewDidLoad];
                    //[myTbc viewWillAppear:YES];
                    //[nvc viewDidLoad];
                    //[nvc viewWillAppear:YES];
                //}
            //}

        }
        else{
            NSLog(@"pause");
        }


    }
}

进入具有 tabBarItem 的视图控制器的 -(void)awakeFromNib 方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多