3

EDIT: I figured out that i needed to remove subviews, once the view did disappear

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

My problem is that I have an UILabel which updates the text correctly, but won't remove the old text. I think maybe it's because there are 2 UILabels on top of each other, here is a picture of it:

enter image description here

And here is my code. I can't see where the duplicate is:

    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];

I hope some of you guys can help me out with this! :)

JMIT
  • 169
  • 2
  • 15

3 Answers3

1

the other way, you should do it, it's remove the label from superView everytime, before you initialize it.

You modify the code like this,

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 = @"";
Bogdan Somlea
  • 604
  • 3
  • 15
0

that happens because you instantiate the UILabel each time, You have to instantiate the it only once, outside the for loop, and then just change the title.

This might work, i haven't tried it

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];
Bogdan Somlea
  • 604
  • 3
  • 15
  • But i have to initialize it because i'm having an uiscrollview with different views inside, which i add the scoring label to at each view – JMIT Jun 30 '14 at 09:33
  • then why don't you use an collectionView with customCells, and in each cell modify the label's title? – Bogdan Somlea Jun 30 '14 at 10:13
0

In similar situation, the property of UIView’s visual appearance helped me. Try this:

self.Scoreint.clearsContextBeforeDrawing = true
Ky6a
  • 1