在我的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];
}
我的故事板:
答案 0 :(得分:2)
离线诊断,我们确认这是在后台线程上收到的。在这种情况下,您可以将通知发布到主队列,也可以让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;
});
}
答案 1 :(得分:0)
我很抱歉,但我真的不明白你的问题:
我想到的是你必须使用viewController并使用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;
});
}
修改强>
然后你去了,我在编辑我的答案时得到了一个缓慢的互联网投票。