2015-05-30 55 views
1

在我的iOS应用程序中,我试图在另一个视图中设置UILabel的文本。无法以编程方式设置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]; 
} 

我的故事板:

enter image description here

+2

是'IBOutlet'勾搭上了?也就是'progressMessage'非''nil'?如果'nil',如果'IBOutlet'没有连接到Interface Builder或视图控制器本身被错误实例化(例如'[[ProcessingViewController alloc] init]'而不是'[self.storyboard instantiateViewControllerWithIdentifier' :...]')。 – Rob

+0

@Rob是和是。我可以记录progressMessage并确认它不是零。此外,我使用故事板连接IBOutlet。我添加了一张图片来展示我如何连接IBOutlet。 – scientiffic

+0

然后确认'progressMessage'的'frame'。确保它的宽度足以显示文字。您可能需要查看整个视图层次结构,以确保框架正确,并且在标签前没有任何内容,从而遮盖它。例如,暂停执行并在'(lldb)'提示符下键入'po [[UIWindow keyWindow] recursiveDescription]'。你也可以尝试内置在Xcode中的有点片状的视图调试器。 – Rob

回答

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; 
    }); 
} 
0

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

是你有什么,我必须考虑到的viewController和你使用NSNotificationCenter传递一个消息从childViewController到parentViewController?那是心病

- (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"]; 

,而我认为你需要做的是使确定你的更新是在主要步行..

编辑

和你去那里,我得到了下来投给有缓慢的互联网,而编辑我的答案..

相关问题