在didReceiveRemoteNotification上的MasterViewController中更新navigationItem.rightBarButtonItem

时间:2014-08-07 05:57:39

标签: ios push-notification uinavigationitem

我的MasterViewController上有一个rightBarButtonItem,当应用程序收到推送通知并通过主屏幕上的应用程序图标打开时,会显示徽章。当应用程序打开时收到通知,我无法在按钮上显示徽章。

AppDelegate.m

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
    NSNumber *badgeNum = [apsInfo objectForKey:@"badge"];
    MasterViewController *masterView = [[MasterViewController alloc] init];
    [masterView updateNotificationButton:badgeNum];
}

MasterViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *barButtonImg = [UIImage imageNamed:@"ico_notification"];
    UIButton *notificationButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
    [notificationButton addTarget:self action:@selector(dealsButton:) forControlEvents:UIControlEventTouchUpInside];
    [notificationButton setImage:barButtonImg forState:UIControlStateNormal];
    BBBadgeBarButtonItem *barButton = [[BBBadgeBarButtonItem alloc] initWithCustomUIButton:notificationButton];
    barButton.badgeOriginX = 13;
    barButton.badgeOriginY = -9;
    NSInteger badgeNum = [UIApplication sharedApplication].applicationIconBadgeNumber;
    barButton.badgeValue = [NSString stringWithFormat:@"%ld", badgeNum];
    self.navigationItem.rightBarButtonItem = barButton;
}

- (void)updateNotificationButton:(NSNumber *)badgeNum {
    BBBadgeBarButtonItem *barButton = (BBBadgeBarButtonItem *)self.navigationItem.rightBarButtonItem;
    barButton.badgeValue = [NSString stringWithFormat:@"%ld", (long)badgeNum];
}

updateNotificationButton:运行时,我可以看到 barButton 为零,但我不确定我做错了什么。

1 个答案:

答案 0 :(得分:0)

使用通知......

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
    {
        NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
        NSNumber *badgeNum = [apsInfo objectForKey:@"badge"];
        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:badgeNum forKey:@"badge"];
       [[NSNotificationCenter defaultCenter] postNotificationName:@"badgeUpdationNotification" object:nil userInfo:userInfo];
    }

然后在您的MasterViewController viewDidLoad方法中观察通知

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImage *barButtonImg = [UIImage imageNamed:@"ico_notification"];
    UIButton *notificationButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
    [notificationButton addTarget:self action:@selector(dealsButton:) forControlEvents:UIControlEventTouchUpInside];
    [notificationButton setImage:barButtonImg forState:UIControlStateNormal];
    BBBadgeBarButtonItem *barButton = [[BBBadgeBarButtonItem alloc] initWithCustomUIButton:notificationButton];
    barButton.badgeOriginX = 13;
    barButton.badgeOriginY = -9;
    NSInteger badgeNum = [UIApplication sharedApplication].applicationIconBadgeNumber;
    barButton.badgeValue = [NSString stringWithFormat:@"%ld", badgeNum];
    self.navigationItem.rightBarButtonItem = barButton;
   //Observe notification here
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateNotificationButton:) name:@"badgeUpdationNotification" object:nil];
}

- (void)updateNotificationButton:(NSNotification *)notification
{
    int badgeNum = [[[notification userInfo] valueForKey:@"badge"] intValue];
    BBBadgeBarButtonItem *barButton = (BBBadgeBarButtonItem *)self.navigationItem.rightBarButtonItem;
    barButton.badgeValue = [NSString stringWithFormat:@"%d", (long)badgeNum];
}