UILabel更新文本,但不会删除旧文本

时间:2014-06-30 08:47:56

标签: ios uilabel updating

编辑:我发现我需要删除子视图,一旦视图消失了

[[self.scrollView subviews]makeObjectsPerformSelector:@selector(removeFromSuperview)];

我的问题是我有一个UILabel可以正确更新文字,但不会删除旧文字。我想也许是因为它有两个UILabels,这是一张图片:

enter image description here

这是我的代码。我无法看到副本的位置:

    for (int i = 0; i < array1.count; i++) {

    NSNumber *myNumber = [myscoretext objectAtIndex:i];
    float myScore = myNumber.floatValue;

    NSNumber *levelNumber = [neededscoretext objectAtIndex:i];
    float levelScore = levelNumber.floatValue;

    for (int i = 0; i < array1.count; i++) {

    NSNumber *myNumber = [myscoretext objectAtIndex:i];
    float myScore = myNumber.floatValue;

    NSNumber *levelNumber = [neededscoretext objectAtIndex:i];
    float levelScore = levelNumber.floatValue;

    float progressScore = ((float)myScore/(float)levelScore);


    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    UIView *subview = [[UIView alloc] initWithFrame:frame];
    subview.backgroundColor = [UIColor clearColor];

    NSArray *colorArray = [NSArray arrayWithObjects:edmeral, turqouise, orange, red, nil];
    // Labeled progress views
    self.labeledProgressView = [[DALabeledCircularProgressView alloc]
                                initWithFrame:CGRectMake(25.0f, 20.0f, 100.0f, 100.0f)];
    self.labeledProgressView.roundedCorners = NO;
    self.labeledProgressView.trackTintColor = [UIColor colorWithWhite:1.0f alpha:0.8];

    imageLevel = [[UIImageView alloc] initWithFrame:CGRectMake(25.0f, 20.0f, 100.0f, 100.0f)];
    imageLevel.backgroundColor = [UIColor clearColor];


    self.Scoreint = [[UILabel alloc] initWithFrame:CGRectMake(-25.0f, 130, 195, 21)];
    self.Scoreint.backgroundColor = [UIColor clearColor];
    self.Scoreint.textColor = [UIColor blackColor];
    [self.Scoreint setFont:[UIFont fontWithName:@"HelveticaNeue-Thin" size:15]];
    self.Scoreint.textAlignment = NSTextAlignmentCenter;
    self.Scoreint.text = @"";


    if (myScore == 0) {

        NSString *scoreString = [NSString stringWithFormat:@"0 / %5ld", (long)levelScore];
        [self.Scoreint setText:scoreString];
    }
    else {

        NSString *scoreString = [NSString stringWithFormat:@"%5li / %5li", (long)myScore, (long)levelScore];
        [self.Scoreint setText:scoreString];

    }

    [subview addSubview:Scoreint];

我希望你们中的一些人可以帮助我解决这个问题! :)

3 个答案:

答案 0 :(得分:1)

另一方面,你应该这样做,它在每次初始化之前都会从superView中删除标签。

您可以像这样修改代码,

if(self.Scoreint) {
[self.Scoreint removeFromSuperview];
}
self.Scoreint = [[UILabel alloc] initWithFrame:CGRectMake(-25.0f, 130, 195, 21)];
self.Scoreint.backgroundColor = [UIColor clearColor];
self.Scoreint.textColor = [UIColor blackColor];
[self.Scoreint setFont:[UIFont fontWithName:@"HelveticaNeue-Thin" size:15]];
self.Scoreint.textAlignment = NSTextAlignmentCenter;
self.Scoreint.text = @"";

答案 1 :(得分:0)

发生这种情况是因为你每次都实例化UILabel,你必须在for循环之外只实例化一次,然后只需更改标题。

这可能有效,我还没有尝试过

self.Scoreint = [[UILabel alloc] initWithFrame:CGRectMake(-25.0f, 130, 195, 21)];
self.Scoreint.backgroundColor = [UIColor clearColor];
self.Scoreint.textColor = [UIColor blackColor];
[self.Scoreint setFont:[UIFont fontWithName:@"HelveticaNeue-Thin" size:15]];
self.Scoreint.textAlignment = NSTextAlignmentCenter;
self.Scoreint.text = @"";

for (int i = 0; i < array1.count; i++) {

NSNumber *myNumber = [myscoretext objectAtIndex:i];
float myScore = myNumber.floatValue;

NSNumber *levelNumber = [neededscoretext objectAtIndex:i];
float levelScore = levelNumber.floatValue;

for (int i = 0; i < array1.count; i++) {

NSNumber *myNumber = [myscoretext objectAtIndex:i];
float myScore = myNumber.floatValue;

NSNumber *levelNumber = [neededscoretext objectAtIndex:i];
float levelScore = levelNumber.floatValue;

float progressScore = ((float)myScore/(float)levelScore);


CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;

UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [UIColor clearColor];

NSArray *colorArray = [NSArray arrayWithObjects:edmeral, turqouise, orange, red, nil];
// Labeled progress views
self.labeledProgressView = [[DALabeledCircularProgressView alloc]
                            initWithFrame:CGRectMake(25.0f, 20.0f, 100.0f, 100.0f)];
self.labeledProgressView.roundedCorners = NO;
self.labeledProgressView.trackTintColor = [UIColor colorWithWhite:1.0f alpha:0.8];

imageLevel = [[UIImageView alloc] initWithFrame:CGRectMake(25.0f, 20.0f, 100.0f, 100.0f)];
imageLevel.backgroundColor = [UIColor clearColor];


[self.Scoreint setFont:[UIFont fontWithName:@"HelveticaNeue-Thin" size:15]];
self.Scoreint.textAlignment = NSTextAlignmentCenter;
self.Scoreint.text = @"";


if (myScore == 0) {

    NSString *scoreString = [NSString stringWithFormat:@"0 / %5ld", (long)levelScore];
    [self.Scoreint setText:scoreString];
}
else {

    NSString *scoreString = [NSString stringWithFormat:@"%5li / %5li", (long)myScore, (long)levelScore];
    [self.Scoreint setText:scoreString];

}

[subview addSubview:Scoreint];

答案 2 :(得分:0)

在类似情况下,UIView的视觉外观特性对我有所帮助。试试这个:

self.Scoreint.clearsContextBeforeDrawing = true