我在我的代码中使用推送通知,每当收到通知时,我想更新另一个 ViewController 中标签的值。
我在 AppDelegate 中的代码是:
- (void)addMessageFromRemoteNotification:(NSDictionary*)userInfo updateUI:(BOOL)updateUI
{
NSLog(@"Notification arrived");
//[mydeals setCode1_id:mydeals.code1_id withString:@"123456"];
mydeals=[[MyDealsViewController alloc]init];
NSDictionary* codeDetails=[[NSDictionary alloc] initWithObjectsAndKeys:@"123456",@"Code_id", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CodeArrived" object:self userInfo:codeDetails];
}
然后在我的另一个视图控制器中,我有以下代码:
@implementation MyDealsViewController
-(id) init
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveCode:)
name:@"CodeArrived"
object:nil];
return self;
}
-(void) receiveCode:(NSNotification*)notification
{
NSLog(@"received Code: %@",notification.userInfo);
self.code1_id.text=[NSString stringWithFormat:@"%@",[[notification userInfo] valueForKey:@"Code_id"]];
}
日志打印正确,但是当我手动进入该屏幕时,我看到了默认值,就像标签根本没有更新一样。我应该怎么办?