How to update UILabel programmatically in iOS

2020-04-11 12:58发布

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++;
    }        
}

标签: ios uilabel
4条回答
三岁会撩人
2楼-- · 2020-04-11 13:46

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.

查看更多
混吃等死
3楼-- · 2020-04-11 13:48

Set clearsContextBeforeDrawing property of your label to YES

you can set this from nib as well as code.

label.clearsContextBeforeDrawing = YES;
查看更多
Deceive 欺骗
4楼-- · 2020-04-11 13:51

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

- (void)setNeedsDisplay
查看更多
Ridiculous、
5楼-- · 2020-04-11 14:01

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

查看更多
登录 后发表回答