[英]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.