0

我在我的应用程序中使用了推送通知,当通知到达时,我将在有效负载中获得一些值,我需要更新我的应用程序中的值。我在所有视图中都有金额标签,所以我需要在收到通知后更新金额。但问题是,该值不会立即更新,当我导航到任何视图之后它会更新。我想立即更新值,那我该怎么做?

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

            NSString *getAmt =[[userInfo valueForKey:@"aps"] valueForKey:@"Amount"];
            float totalAmt = [getAmt floatValue] + [amt floatValue];
            NSString *finalAmount = [NSString stringWithFormat:@"%.02f",totalAmt];
            [[NSUserDefaults standardUserDefaults] setObject:finalAmount forKey:@"amount"];
            [self.viewController.view setNeedsDisplay];
}

我使用 Nsuserdefault 值在金额标签中显示金额。现在值已更改,当我导航到任何视图时,它将更新。但是我想在收到通知后立即在我的应用程序中更新该值。所以请帮帮我。

谢谢!

4

2 回答 2

1

使用 NSNotication 并针对实现方法的类的方法添加通知,当您收到推送通知时,发布该通知并在方法中更新值

于 2012-05-08T11:34:29.367 回答
1

好吧,您可以NSNotificationCenter用于此目的..

像这样注册您想要UIViewControllerNSNotificationCenter@"NotificaitonRecivied"

[[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector: @selector(updateAmount:)
                                                     name: @"NotificaitonRecivied" 
                                                   object: nil];

现在,每当您收到任何更新的金额时,只需广播如下通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificaitonRecivied" object:nil];

updateAmount:amount 将是一种读取您的金额NSUserDefaults以将其设置在任何View

于 2012-05-08T11:37:36.367 回答