How to update UILabel programmatically in iOS

12,903

Solution 1

If you set the text of your label you do not need to call setNeedsDisplay or clearContext etc.

In your code, I do not know what are your variables i and x?

The main problem is that you are creating and adding new labels on your view. When you call updateLabels method, may cause a Memory leak. Simply you have n times labels overlapped.

You need to remove the labels before you create and add new labels or you can reuse which you already have. To reuse your labels you need to save them to an array and update texts.

If you want to create new labels then you can do like this unless you have other labels in your view

-(void) updateLabels{
// remove all labels in your view   
for (UIView *labelView in self.view.subviews) {
    if ([labelView isKindOfClass:[UILabel class]]) {
    [labelView removeFromSuperview];
}

for (GraphView *graph in arr){
    // retrieve the graph values
    valLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * 200, 0, 90, 100)];
    valLabel.textColor = [UIColor whiteColor];
    valLabel.backgroundColor = [UIColor clearColor];
    valLabel.text = [NSString stringWithFormat:@"Value: %f", x];
    i++;
}        
}

When you create new labels like this you need to add them to your view as subview

[self.view addSubview: valLabel];

if you have other labels in your view then you can save them in an array and remove just them

Solution 2

You need to call setNeedsDisplay so that the app knows it has changed and redraw it.

- (void)setNeedsDisplay

Solution 3

Your updateLabels method is actually creating new UILabel controls each time so they will simply appear "on top of" older ones. I'm guessing this is not what you want, although it's not perfectly clear so apologies if I've misunderstood what you're trying to do.

If I'm correct about that, create your UILabel controls just once maybe in your viewDidLoad or similar. Then just set their .text properties when your timer fires.

Solution 4

Set clearsContextBeforeDrawing property of your label to YES

you can set this from nib as well as code.

label.clearsContextBeforeDrawing = YES;
Share:
12,903
user123
Author by

user123

Updated on June 05, 2022

Comments

  • user123
    user123 almost 2 years

    I am facing a problem with updating my labels.. it doesn't remove the old values so new values go on top of old ones.. any help with this will be appreciated..

    timer = [NSTimer scheduledTimerWithTimeInterval:1
                                             target:self
                                           selector:@selector(updateLabels)
                                           userInfo:nil
                                            repeats:YES];
    
    -(void) updateLabels{
        for (GraphView *graph in arr){
            // retrieve the graph values
            valLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * 200, 0, 90, 100)];
            valLabel.textColor = [UIColor whiteColor];
            valLabel.backgroundColor = [UIColor clearColor];
            valLabel.text = [NSString stringWithFormat:@"Value: %f", x];
            i++;
        }        
    }
    
  • user123
    user123 over 11 years
    is it something like [valLabel setNeedsDisplay];
  • user123
    user123 over 11 years
    yes you are right.. i used to create them in my viewDidLoad method but if i had multiple labels, only the last label could be updated.. I dont know how to fix this problem..
  • Sreeram
    Sreeram over 11 years
    You can assign tags to labels if there are many labels on the screen like this valLabel.tag=i.apologies if I've misunderstood what you're trying to do
  • Mert
    Mert over 11 years
    if you set the text of UILabel, it redraws automatically. You do not need to call the setNeedsDisplay
  • Martin
    Martin over 11 years
    When you create them in your viewDidLoad method you need to keep a reference to each one of them in your class. Maybe use an array to store all those UILabel objects. If you only keep a single UILabel reference then yes, only the last one can be changed.
  • user123
    user123 over 11 years
    I am getting an error.. it says ` No visible @interface for 'UIView' declares the selector 'removeFromSuperView'
  • Mert
    Mert over 11 years
    It should be removeFromSuperview. just wrong typed. I fix my answer as well