[英]Can't access and set Text of UILabel in a programmatically loaded XIB
[英]Can't set UILabel Text Programmatically
在我的iOS应用中,我试图在另一个视图中设置UILabel的文本。
我通过在应更新时将NSNotification传递给viewController来实现。 我知道它是因为我记录了消息而正确接收到消息,但是它只是没有出现在UILabel中(我在情节提要中添加了它)。
这是我的代码:
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];
}
我的故事板:
诊断此脱机状态后,我们确认实际上是在后台线程上收到的。 在这种情况下,您可以将通知发布到主队列,也可以让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;
});
}
对不起,但我不太了解您的问题:
我要记住的是,您必须使用viewViewController并使用NSNotificationCenter
将消息从childViewController传递到parentViewController吗? 那是cor
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateProgressDialog:) name:@"uploadProgress" object:nil];
}
但是如果您想使用NSNotificationCenter
将消息发送回去
//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"];
而我认为您需要做的就是确保您的更新处于主要阶段。
-(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;
});
}
编辑
然后你去了,我在编辑我的答案时因为互联网速度慢而被否决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.