Search code examples
iosobjective-cuilabel

Can't set UILabel Text Programmatically


In my iOS app, I'm trying to set the text of a UILabel in another view.

I do this by passing a NSNotification to the viewController when it should be updated. I know it is receiving the message correctly because I log the message, but it just isn't appearing in the UILabel (which I added in the storyboard).

This is my code:

ProcessingViewController.h

@property (weak, nonatomic) IBOutlet UILabel *progressMessage;

ProcessingViewController.m

- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateProgressDialog:) name:@"uploadProgress" object:nil];
}

-(void) updateProgressDialog: (NSNotification *) notification{
    NSString *message = [notification object];
    NSLog(@"received updateProgressDialog message of %@", message);
    self.progressMessage.text = message;
    [self.progressMessage setNeedsDisplay];
}

My storyboard:

enter image description here


Solution

  • Diagnosing this offline, we confirmed that this was really being received on a background thread. In that case, you can either post the notification to the main queue, or you can have updateProgressDialog dispatch the UI updates to the main queue:

    -(void) updateProgressDialog: (NSNotification *) notification{
        NSString *message = [notification object];
        NSLog(@"received updateProgressDialog message of %@", message);
        dispatch_async(dispatch_get_main_queue(), ^{
            self.progressMessage.text = message;
        });
    }