Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在通知回调中设置时,标签栏项目上不显示徽章_Ios_Objective C_Cocoa Touch_Uitabbar_Uitabbaritem - Fatal编程技术网

Ios 在通知回调中设置时,标签栏项目上不显示徽章

Ios 在通知回调中设置时,标签栏项目上不显示徽章,ios,objective-c,cocoa-touch,uitabbar,uitabbaritem,Ios,Objective C,Cocoa Touch,Uitabbar,Uitabbaritem,当我的应用程序收到通知时,我正在尝试将我的选项卡栏中的徽章设置为第四个选项卡上的“1”。奇怪的是,我的控制台说该方法正在启动(请参阅NSLog“Fire!”),但是一旦收到通知,该标记就不会出现在我的选项卡栏项目上?我错过什么了吗 AppDelegate.m -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo UIApplicatio

当我的应用程序收到通知时,我正在尝试将我的选项卡栏中的徽章设置为第四个选项卡上的“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!");
    }
}
- (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];

}
ViewController.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!");
    }
}
- (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];

}

[[self.tabBarController.tabBar.items objectAtIndex:0]setBadgeValue:nil];将计数或数字设置为零,以显示在选项卡栏上


试试这个,它可能会帮助您

[[self.tabBarController.tabBar.items objectAtIndex:0]setBadgeValue:nil];将计数或数字设置为零,以显示在选项卡栏上


尝试此方法可能会帮助您

无需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!");
    }
}

不需要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!");
    }
}

几点建议:从
viewDidLoad
调用
[[self.tabBarController.tabBar.items objectAtIndex:4]setBadgeValue:@“1”]
时会发生什么?这行是从主线程调用的吗?你有更多的。。。tab可能?“包含>=5项”
objectAtIdex:
会引发越界索引异常。请尝试此[[self.tabBarController.tabBar.items objectAtIdex:2]setBadgeValue:@“1”];1是否出现在硬编码索引中?请注意,您正在尝试设置第5个选项卡,而不是第4个。@BashirSidani尝试过这个,但仍然没有出现“1”。尽管如此,我的NSLog“Fire!”仍然显示在控制台中,这让我相信该方法已经执行。一些建议:调用
[[self.tabBarController.tabBar.items objectAtIndex:4]setBadgeValue:@“1”]
from
viewDidLoad
时会发生什么?这行是从主线程调用的吗?你有更多的。。。tab可能?“包含>=5项”
objectAtIdex:
会引发越界索引异常。请尝试此[[self.tabBarController.tabBar.items objectAtIdex:2]setBadgeValue:@“1”];1是否出现在硬编码索引中?请注意,您正在尝试设置第5个选项卡,而不是第4个。@BashirSidani尝试过这个,但仍然没有出现“1”。尽管如此,我的NSLog“Fire!”仍然显示在控制台中,这让我相信该方法已经执行。