Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 31, 2008, 12:40 AM   #1
jk7
macrumors newbie
 
Join Date: Mar 2008
iPhone UILabel not updating

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

Code:
#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
Attached Files
File Type: zip foo.zip (14.8 KB, 253 views)

Last edited by dejo; Nov 11, 2010 at 08:44 AM. Reason: Please use [code] tags.
jk7 is offline   0 Reply With Quote
Old Mar 31, 2008, 11:40 AM   #2
jk7
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
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.

Last edited by dejo; May 9, 2011 at 08:17 AM. Reason: Cleanup.
jk7 is offline   0 Reply With Quote
Old Apr 3, 2008, 10:49 PM   #3
therevolution
macrumors 6502
 
Join Date: May 2003
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.

Last edited by dejo; May 9, 2011 at 08:18 AM. Reason: Cleanup.
therevolution is offline   0 Reply With Quote
Old Apr 3, 2008, 11:14 PM   #4
jk7
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
reply was deleted

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

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

Last edited by dejo; May 9, 2011 at 08:18 AM. Reason: Please use [code] tags.
jk7 is offline   0 Reply With Quote
Old Apr 3, 2008, 11:36 PM   #5
therevolution
macrumors 6502
 
Join Date: May 2003
Awesome, thanks. That did it. You get a gold star.
therevolution is offline   0 Reply With Quote
Old Jul 31, 2009, 04:04 PM   #6
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
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.
jared_kipe is offline   0 Reply With Quote
Old Nov 10, 2010, 07:23 PM   #7
Bagnaria
macrumors newbie
 
Join Date: Nov 2010
Contents of Nib files are not always available

One thing to be aware of with iPhone applications is that one can not rely on UI elements that where initialized from a nib file. The reason is that Cocoa Touch tries to save as much memory as possible and only instantiates the UI elements of a nib file when they are visible. Then it tosses them only to re-instantiate them should the interface become visible again. So while the "Your"-ViewController class is instantiated you can not rely on the IBOutlets to be there. An easy check in the Debugger is that the label (or other UI element) will be null/nil. This has a lot to do with timing so what works in one place may not work in another. Which why this is so hard to catch.

The solution:
While the IBOutlets may not always be instantiated, all other instance variables will be persistent. So in this example I would simply add an NSString ivar, let's call it "labelString". The "labelString" can be set as soon as your controller has been initialized (or right then). When it is showtime the viewDidLoad method will be called. It should look similar to this like this:

Code:
 - (void)viewDidLoad {
    uiLabel.text = labelString; // set the label when it is actually shown

    [super viewDidLoad];
}
Hope this helps. It is a behaviour that can be very frustrating, but once you are aware of it you will be much happier.

Last edited by dejo; Nov 11, 2010 at 08:45 AM. Reason: Please use [code] tags.
Bagnaria is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Writing value to UILabel Kapthehat iPhone/iPad Programming 6 Jul 19, 2013 08:32 AM
Can't move a IBOutlet UILabel position hontrok iPhone/iPad Programming 6 Nov 14, 2012 06:41 AM
UILabel Help lukin iPhone/iPad Programming 3 Sep 18, 2012 07:36 PM
sizeToFit() for UILabel is moving left. Narendar Singh iPhone/iPad Programming 10 Jul 11, 2012 10:11 AM
Stupid UILabel question mngoose iPhone/iPad Programming 7 Jun 11, 2012 08:58 PM

Forum Jump

All times are GMT -5. The time now is 05:06 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC