ios - 通知中心被调用但 UILabel 的值未在 IOS 中更新

标签 ios notifications nsnotificationcenter

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

然后在我的另一个 View Controller 中我有以下代码:

@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。否则它将使用默认值进行初始化。

关于ios - 通知中心被调用但 UILabel 的值未在 IOS 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17472629/

相关文章:

ios - 调试在 XCode 下不会重现的 NSNotification 崩溃

xcode - Swift 3 - 如何使用枚举原始值作为 NSNotification.Name?

iphone - Objective-C JSON - 将 JSON 对象转换为 native 对象?

iOS spriteNode 背景图片 ios 7 和 ios 8 的区别

javascript - 移动网络应用程序上的 keyup 似乎仍然没有覆盖内容 block

react-native - react native 本地通知

ios - Swift:没有调用didRegisterForRemoteNotificationsWithDeviceToken

cocoa - 滥用 NSNotificationCenter

ios - 在 iOS 中出现键盘时启用滚动内容

iphone - iOS - 更新 applicationDidBecomeActive 中的 View Controller