![](/img/trans.png)
[英]How to set a UITabBarController as rootViewController from Storyboard
[英]How to set badge for UIBarButtonItem from Appdelegate (whenever push notification comes) if UITabbarcontroller is not the rootviewcontroller?
如果UITabbarcontroller不是rootviewcontroller,如何從Appdelegate設置UIBarButtonItem的徽章(無論何時推送通知)?
我在UITabbarcontroller之前有一個LoginViewController和PinViewController。 如果用戶尚未登錄,我將LoginViewController設置為rootviewcontroller,如果用戶已經登錄,則將PinViewController設置為rootviewcontroller。但是我發現,只有在rootviewcontroller是UITabbarcontroller的情況下,才可以將Appdelegate的UIBarButtonItem設置為徽章。
誰能幫我這個?
您可以通過發布通知進行管理
//在App Delegate類中
-(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...
}
}
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.