Ios Can';t以编程方式设置UILabel文本

Ios Can';t以编程方式设置UILabel文本,ios,objective-c,uilabel,Ios,Objective C,Uilabel,在我的iOS应用程序中,我试图在另一个视图中设置UILabel的文本 我通过在应该更新时向viewController传递通知来实现这一点。我知道它正在正确地接收消息,因为我记录了消息,但它只是没有出现在UILabel中(我在脚本中添加了UILabel) 这是我的代码: 处理ViewController.h @property (weak, nonatomic) IBOutlet UILabel *progressMessage; 处理ViewController.m - (void)view

在我的iOS应用程序中,我试图在另一个视图中设置UILabel的文本

我通过在应该更新时向viewController传递通知来实现这一点。我知道它正在正确地接收消息,因为我记录了消息,但它只是没有出现在UILabel中(我在脚本中添加了UILabel)

这是我的代码:

处理ViewController.h

@property (weak, nonatomic) IBOutlet UILabel *progressMessage;
处理ViewController.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];
}
我的故事板:


很抱歉,我不太明白你的问题:

我想的是,您必须查看控制器,并使用
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;
    });
}
编辑


好了,我在编辑我的答案时,因为网速太慢而被否决了

离线诊断时,我们确认这确实是在后台线程上收到的。在这种情况下,您可以将通知发布到主队列,也可以让
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;
    });
}

IBOutlet
连接好了吗?即,
progressMessage
是否为非
nil
?如果
nil
,如果
IBOutlet
未连接到Interface Builder中,或者视图控制器本身实例化不正确(例如
[[ProcessingViewController alloc]init]
而不是
[self.storyboard InstanceDeviceController WithiIdentifier:…]
),则可能发生这种情况。@Rob yes和yes。我能够记录progressMessage并确认它不是零。此外,我还使用故事板连接了IBOutlet。我添加了一个图像来显示我是如何连接IBOutlet的。然后也确认
progressMessage
frame
。确保它足够宽以显示文本。您可能希望查看整个视图层次,以确保框架正确,并且标签前面没有遮挡标签的东西。例如,暂停执行并在
(lldb)
提示符处键入
po[[UIWindow keyWindow]recursiveDescription]
。您还可以尝试Xcode中内置的有点古怪的视图调试器。@rob我移动了标签以确保它没有被UIImage阻止,但我仍然看不到它。我还确保没有检查静态文本。我应该寻找uilabel的其他属性吗?不要依赖IB。当应用程序运行时,请查看
框架。另外,我假设您是从主线程发布此通知的?