0

我在我的代码中使用推送通知,每当收到通知时,我想更新另一个 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"]];
}

日志打印正确,但是当我手动进入该屏幕时,我看到了默认值,就像标签根本没有更新一样。我应该怎么办?

4

2 回答 2

1

你必须确保当你“手动去”时MyDealsViewController,不管你怎么做,它必须是同一个MyDealsViewController被调用的 wich实例receiveCode。否则它将使用它的默认值初始化。

于 2013-07-04T14:24:36.937 回答
0

您也可以尝试调用 [self.code1_id setNeedsLayout];

于 2013-07-04T14:41:58.713 回答