PDA

View Full Version : iPhone UILabel not updating




jk7
Mar 31, 2008, 01:40 AM
I'm writing an app for the iPhone and I'm finding that the UILabel is not updating. A worker thread updates a string and calls a method on the view that changes the text in a UILabel subview.

I'm not seeing the new values appear in the UILabel.

Here's the code for the view, the updateDisplay method is called by the worker thread.

Anybody know why the UILabel doesn't get updated?

I attached the whole project.

Thx

#import "MyView.h"

@implementation MyView

@synthesize label;

-(id)initWithFrame:(CGRect)aRect
{
if (![super initWithFrame:aRect]) return nil;

self.backgroundColor = [UIColor whiteColor];

CGRect labelFrame = CGRectInset(aRect, 90, 90);

self.label = [[UILabel alloc] initWithFrame:labelFrame];
self.label.contentMode = UIViewContentModeRedraw;
[self addSubview: self.label];
[self bringSubviewToFront:self.label];
self.label.text = @"initial value";

return self;
}

-(void) updateDisplay:(NSString *)str
{
self.label.text = str;
[self.label setNeedsDisplay];
//[self setNeedsDisplay];
//[self.window setNeedsDisplay];
}
@end



jk7
Mar 31, 2008, 12:40 PM
Thx,

I'll try that.

I added a setNeedsDisplay after I noticed that the UILabel was not updating.

Also if anybody runs that project be sure to exit the application by pressing the home button.

I've crashed my mbp a number of times debugging that project and think it might be runaway threads.

therevolution
Apr 3, 2008, 11:49 PM
Either a reply was deleted, or you're talking to yourself... :D

I am actually having a similar problem with cells in a UITableView. Updating a UILabel might be similar. Did you get it resolved, or does anyone else know the trick to updating the UI?

It'll update the contents if I touch the screen, but not if it's left alone. I've tried several variations of [table reloadData], [table setNeedsLayout], [table setNeedsDisplay], doing the same to each of the cells... no luck so far.

jk7
Apr 4, 2008, 12:14 AM
Replay was deleted.

Wasn't talking to myself (I don't think anyway).

Answer was (I'm summarizing) updates must be on the main thread.

This code is working for me in the above project, currently having problems crashing mbp so wouldn't recomend running the project attached to this thread

-(void) updateDisplay : ( NSString *)str
{
[self.label performSelectorOnMainThread : @ selector(setText : ) withObject:str waitUntilDone:YES];
}

therevolution
Apr 4, 2008, 12:36 AM
Awesome, thanks. That did it. You get a gold star.

jared_kipe
Jul 31, 2009, 05:04 PM
You shouldn't need to call self.label.text, since label is an ivar of your own class.

label.text = str;

SHOULD be fine. I've never created a view like this programatically, I always use xib files and then move or change properties as I need to. I wouldn't think this would change how the UIView subclasses like UILabel or UIImageView redraw themselves when changed.

It will not redraw until the method that makes the change returns though.
I use similar methods to update the main view after changing triangles in the Right Triangle Tutor app I have on the store currently.