Search code examples
objective-ciosuilabelnsnotificationcenter

Updating a UILabel from another view using NSNotificationCenter


I'm updating a UILabel in another view using a method invoked using the NSNotification center.

What I'm finding is that it's not working, the label remains unchanged until the view is reloaded at a later point (not using the method called by nsnotification center).

The label is set to nonatomic, retain and I'm using self to bind everything to that property.

obviously all of the code (the method) that tries to update the label is located in the corresponding viewController. The method gets called (I can see that with NSLog), and all it does is self.label2.text = @"label 2 updated";

I'm able to update the text value of the label using a timer just fine, so I don't think there is a problem with the label, but rather with my understanding of threading and/or what is loaded or not at a given time.

What do you think is the problem?

PS: i WILL chose an answer even if it doesn't help me that much, so please post your wildest guesses!


Solution

  • First make sure that your label is not nil when you set the text.

    If it's not nil, try calling setNeedsLayout on your label after setting the text (this shouldn't be necessary). Also, verify that you are on the main thread with:

    [NSThread isMainThread]
    

    If that doesn't help, use a subclass of UILabel, override setText and put a breakpoint on it to see what's going on.