繁体   English   中英

通知中心被调用,但IOS中UILabel的值未更新

[英]Notification Center gets called but value of UILabel is not updated in IOS

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

日志可以正确打印,但是当我手动进入该屏幕时,会看到默认值,例如标签根本没有更新。 我该怎么办?

你必须确保当你“手动走出去” MyDealsViewController ,你是怎么做到的,它得到了什么是相同的实例MyDealsViewController至极被称为receiveCode 否则,它将使用默认值进行初始化。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM