0

Any reason why my label won't update?

- (void)setLabel:(NSNotification*)notification {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"setLabel");
        [self.label setText:@"My Label"];
    });
}

I've also tried using performSelectorOnMainThread to no avail.

Note that setLabel is appears on the log.

Additional info:

I also have two other functions which does the same thing but only with a different text. The two other functions doesn't have dispatch_async but they both work. Also, the notification of the two working function was sent by NSURLConnection (method #2 in this post). While the notification of the non working function above, was sent by a call to FBRequestConnection (see this post).

For clarity, my two other working functions is as follows:

- (void)setLabel2:(NSNotification*)notification {
    NSLog(@"setLabel2");
    [self.label setText:@"My Label 2"];
}    
- (void)setLabel3:(NSNotification*)notification {
    NSLog(@"setLabel3");
    [self.label setText:@"My Label 3"];
}

Yes I did try to remove dispatch_async in my code. In fact, originally there was no dispatch_async because the other two were working.

Community
  • 1
  • 1
ian
  • 1,505
  • 2
  • 17
  • 24
  • Have you tried temporarily removing the dispatch_async to see if the label is set regardless? – jfuellert Jun 08 '13 at 14:17
  • make sure you work with correct label and it is set as a correct outlet from your Storyboard – Oleg Jun 08 '13 at 14:24
  • @Kreiri - if it was nil, shouldn't it crash the app? My doesn't crash after calling that line. – ian Jun 09 '13 at 01:26
  • @Jfuellert - it originally doesn't have dispatch_async, I added it to make sure it updates but it doesn't. – ian Jun 09 '13 at 01:32
  • @Oleg - it's the only one label. There are other codes which calls the same code and they work. – ian Jun 09 '13 at 01:35
  • 1
    Sending a message to `nil` is legal in Objective-C. It does nothing. You need to figure out why `self.label` is nil. – rob mayoff Jun 09 '13 at 02:57
  • @robmayoff - unfortunately it's not nil. :( – ian Jun 09 '13 at 05:51
  • The next common cause is that `self.label` points to something other than the label on the screen. Try calling `[self.label setText:]` elsewhere, such as in `viewDidLoad`, and make sure it actually updates and using `NSLog` verify that they are the same address. – Rob Napier Jun 09 '13 at 14:51
  • @RobNapier - as I said in my question, I also have 2 other functions which does the same thing and they both work. I'll edit my question again for clarity. – ian Jun 09 '13 at 23:01
  • Yes, I saw that. Add the check that self.label has the same address in all these cases. You are either not updating the object you think you are, or the object you are updating is not on the screen, or you are not on the main thread. These are the reasons for the failure you're describing. Figuring out which is the actual cause is how you debug it. – Rob Napier Jun 11 '13 at 06:18
  • @RobNapier thanks Rob for your answers. I've confirmed that in all 3 functions, self.label has the same memory address. I can also confirm that self.label is on the screen because there's a text showing on it. How do you make sure you are on the main thread? When I debug all the 3 functions, all of them shows on thread 1. The only difference I can see is on the stack. The non working function is notified using FBRequestConnection while the other 2 working function is notified by NSURLConnection. Any other possible reason why it won't update? – ian Jun 11 '13 at 11:41
  • So far you've made certain that **a** label is on the screen by checking the text showing. That does not mean it is **self.label**. The most likely issue is still that what you're seeing is not the same object you are trying to change. Check the view hierarchy (superview.subviews) and make sure there aren't extra copies of yourself. – Rob Napier Jun 11 '13 at 14:29
  • @RobNapier how do I Check the view hierarchy (superview.subviews)? – ian Jun 13 '13 at 01:58
  • po self.view.superview.subviews – Rob Napier Jun 13 '13 at 04:54
  • @RobNapier - On watch window self.view exists but self.view.superview does not. I've also checked self.label.view but it does not exists. Any other ideas? – ian Jun 13 '13 at 05:46

1 Answers1

3

If you set a break point right after where you set the text and print description of self.label does it show the text has changed? If so it must be getting reset somewhere else after this method fires. If self.label is nil than there's your problem

anders
  • 4,168
  • 2
  • 23
  • 31