【发布时间】: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