【问题标题】:Badge not appearing on tab bar item when set in notification callback在通知回调中设置时,标签不会出现在标签栏项目上
【发布时间】:2017-07-28 17:49:24
【问题描述】:

当我的应用收到通知时,我正在尝试将标签栏中的徽章设置为第四个标签上的“1”。奇怪的是,我的控制台说该方法正在触发(请参阅 NSLog “Fire!”),但是一旦收到通知,我的标签栏项目上就不会出现徽章?我错过了什么吗?

AppDelegate.m

-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo 

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];

        NSLog(@"App notification received!");
        // do stuff when app is active

    }else{
        // do stuff when app is in background

        static int i=1;
        [UIApplication sharedApplication].applicationIconBadgeNumber = i++;

        NSLog(@"App notification received background!");
    }
}

ViewController.m

- (void) myNotificationReceived:(NSNotification *) notification {


    [[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"1"];


    NSLog(@"Fire!");
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myNotificationReceived:) name:@"pushNotification" object:nil];

}

【问题讨论】:

  • 几点建议:当您从viewDidLoad 调用[[self.tabBarController.tabBar.items objectAtIndex:4] setBadgeValue:@"1"] 时会发生什么?这条线是从主线程调用的吗?你有更多...标签吗?
  • "contains >= 5 items" objectAtIdex: 抛出越界索引异常。
  • 试试这个 [[self.tabBarController.tabBar.items objectAtIndex:2] setBadgeValue:@"1"]; 1 是否出现在硬编码索引上?
  • 请注意,您正在尝试设置第 5 个选项卡,而不是第 4 个。
  • @BashirSidani 试过这个,仍然没有“1”出现。不过,我的 NSLog “着火了!”仍然显示在控制台中,让我相信该方法已执行。

标签: ios objective-c cocoa-touch uitabbar uitabbaritem


【解决方案1】:

[[self.tabBarController.tabBar.items objectAtIndex:0] setBadgeValue:nil];将您的计数或数字设置为 nil 以显示在标签栏上

试试这个可能对你有帮助

【讨论】:

    【解决方案2】:

    不需要 NSNotification 方法 - 以下解决了 AppDelegate.m 中的问题:

    -(void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
        UIApplicationState state = [application applicationState];
        if (state == UIApplicationStateActive) {
    
            [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
    
            UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
            [[tabController.viewControllers objectAtIndex:4] tabBarItem].badgeValue = @"1";
    
    
            NSLog(@"App notification received!");
            // do stuff when app is active
    
        }else{
    
    
            static int i=1;
            [UIApplication sharedApplication].applicationIconBadgeNumber = i++;
    
            //  NSLog(@"App notification received!");
    
            NSLog(@"App notification received background!");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-03
      • 1970-01-01
      • 2018-11-16
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      相关资源
      最近更新 更多