[英]Can't set UILabel Text Programmatically
In my iOS app, I'm trying to set the text of a UILabel in another view. 在我的iOS应用中,我试图在另一个视图中设置UILabel的文本。
I do this by passing a NSNotification to the viewController when it should be updated. 我通过在应更新时将NSNotification传递给viewController来实现。 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). 我知道它是因为我记录了消息而正确接收到消息,但是它只是没有出现在UILabel中(我在情节提要中添加了它)。
This is my code: 这是我的代码:
ProcessingViewController.h ProcessingViewController.h
@property (weak, nonatomic) IBOutlet UILabel *progressMessage;
ProcessingViewController.m 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: 我的故事板:
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: 在这种情况下,您可以将通知发布到主队列,也可以让updateProgressDialog
将UI更新分派到主队列:
-(void) updateProgressDialog: (NSNotification *) notification{
NSString *message = [notification object];
NSLog(@"received updateProgressDialog message of %@", message);
dispatch_async(dispatch_get_main_queue(), ^{
self.progressMessage.text = message;
});
}
I'm sorry but i don't really understand your question: 对不起,但我不太了解您的问题:
What i have in mind is you have to viewController and your passing a message from the childViewController to the parentViewController using NSNotificationCenter
? 我要记住的是,您必须使用viewViewController并使用NSNotificationCenter
将消息从childViewController传递到parentViewController吗? Is that cor 那是cor
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateProgressDialog:) name:@"uploadProgress" object:nil];
}
but if you want to send the message back the message using NSNotificationCenter
但是如果您想使用NSNotificationCenter
将消息发送回去
//childViewController.m //childViewController.m
//i tried adding this to your code to test if the `NSNotificationCenter ` responds, and it is responding
[[NSNotificationCenter defaultCenter] postNotificationName:@"uploadProgress" object:@"Your message"];
while i think what you need to do is to make sure the your update is at the main tread.. 而我认为您需要做的就是确保您的更新处于主要阶段。
-(void) updateProgressDialog: (NSNotification *) notification{
NSString *message = [notification object];
NSLog(@"received updateProgressDialog message of %@", message);
dispatch_async(dispatch_get_main_queue(), ^(void){
self.progressMessage.text = message;
});
}
Edit 编辑
and there you go, i got a down vote for having a slow internet while editing my answer.. 然后你去了,我在编辑我的答案时因为互联网速度慢而被否决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.