ios - 无法以编程方式设置 UILabel 文本

标签 ios objective-c uilabel

在我的 iOS 应用中,我试图在另一个 View 中设置 UILabel 的文本。

我通过在应该更新时将 NSNotification 传递给 viewController 来做到这一点。我知道它正在正确接收消息,因为我记录了消息,但它没有出现在 UILabel 中(我在 Storyboard中添加了它)。

这是我的代码:

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];
}

我的 Storyboard:

enter image description here

最佳答案

离线诊断后,我们确认这确实是在后台线程上收到的。在这种情况下,您可以将通知发布到主队列,或者您可以让 updateProgressDialog 将 UI 更新分派(dispatch)到主队列:

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

关于ios - 无法以编程方式设置 UILabel 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30548152/

相关文章:

objective-c - 什么可能导致无法在一台计算机上启动 OS X 应用程序的两个副本?

iphone - 即使使用字符串初始化后,NSURL对象也为零

objective-c - NSTextView 封闭 ScrollView 在空格键上跳转

swift - 尝试设置标签文本时出错

iphone - 在 Webview 中加载 .xls Ms-Excel 文件显示错误

ios - Firebase 收到推送通知时没有收到弹出窗口

ios - 将存储在字符串中的句子的单词初始化到 UILabels 中

ios - 如何将 UILabel 添加为 UIButton iOS 的 subview

ios - NSInvalidArgumentException "Unrecognized selector sent to instance"(使用 MPMoviePlayerController)

ios - NSDecimalNumber decimalNumberWithString : ignores current locale