【问题标题】:How to set badge for UIBarButtonItem from Appdelegate (whenever push notification comes) if UITabbarcontroller is not the rootviewcontroller?如果 UITabbarcontroller 不是 rootviewcontroller,如何从 Appdelegate 为 UIBarButtonItem 设置徽章(无论何时推送通知)?
【发布时间】:2015-08-31 09:54:21
【问题描述】:
如果 UITabbarcontroller 不是 rootviewcontroller,如何从 Appdelegate 为 UIBarButtonItem 设置徽章(无论何时推送通知)?
我在 UITabbarcontroller 之前有一个 LoginViewController 和一个 PinViewController。如果用户尚未登录,我将 LoginViewController 设置为 rootviewcontroller,如果用户已经登录,我将 PinViewController 设置为 rootviewcontroller。但我发现只有当 rootviewcontroller 是 UITabbarcontroller 时,我们才能从 Appdelegate 为 UIBarButtonItem 设置徽章。
谁能帮我解决这个问题?
【问题讨论】:
标签:
ios
objective-c
xcode
ios8
【解决方案1】:
您可以通过发布通知
来管理它
//在应用代理类中
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSNumber *count = [NSNumber numberWithInt:5]; // This is static...
NSDictionary *dataDict = [NSDictionary dictionaryWithObject:count
forKey:@"count"];
application.applicationIconBadgeNumber = 0;
if (application.applicationState == UIApplicationStateActive)
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"getPushNotification" object:dataDict];
}
}
在控制器中,您要在栏上更新徽章计数。
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateBadgeCount:)
name:@"getPushNotification"
object:nil];
}
- (void)updateBadgeCount:(NSNotification *)note {
NSDictionary *theData = [note userInfo];
if (theData != nil) {
NSNumber *n = [theData objectForKey:@"count"];
// Do your stuff here...
}
}
谢谢。