在我的 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:
最佳答案
离线诊断后,我们确认这确实是在后台线程上收到的。在这种情况下,您可以将通知发布到主队列,或者您可以让 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/