2

I am printing a label programmatically but I can't remove it from the screen. I have tried removeFromSuperview and lbl1.hidden = YES; and lbl1= nil; but non of them work. It stays on the screen all the time while I can see in the debug it pass from ELSE as in the code below.

Where would be my problem?

-(void)reloadData
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)];

if (result1 > result2 &&  al == YES)
{
    lbl1.userInteractionEnabled = YES;
    lbl1.text = @" Warning!! ";
    lbl1.tag = 30;
    lbl1.font = [UIFont fontWithName:@"Helvetica" size:18.0];
    lbl1.textColor = [UIColor redColor];
    lbl1.backgroundColor = [UIColor clearColor];
    lbl1.lineBreakMode = NSLineBreakByWordWrapping;
    lbl1.numberOfLines = 2;
    [self addSubview:lbl1];
    [lbl1 release];
}

else{

    //Non of them is removing the label.
    [lbl1 removeFromSuperview];
    lbl1= nil;
    lbl1.hidden = YES;
}
Luai Kalkatawi
  • 1,492
  • 5
  • 25
  • 51
  • in this case i think your method call multiple time please debug your code – kb920 Dec 05 '13 at 12:30
  • Why do you init the label before the first `if` Since your don't add it to a view (addSubview), it's not on the screen. But, I think, that's because you ALWAYS do an alloc/init... So, do the alloc/init if it doesn't exist only. – Larme Dec 05 '13 at 12:31
  • @Luai Kalkatawi - Whether my answer is worked for you. – Muthu Sabarinathan Dec 05 '13 at 13:34

3 Answers3

4

Every time you go into reloadData, you are creating a new label, so if you go into reload and jump to the else, you are creating a label, and then removing it.

You need to save that label as an instance variable and remove it/add it in your reloadData.

@property(nonatomic, strong) UILabel *lbl1;

And in your code, do this only ONCE:

self.lbl1 = [[[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)] autorelease];

And in your reloadData do:

-(void)reloadData
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)];

if (result1 > result2 &&  al == YES)
{
    self.lbl1.userInteractionEnabled = YES;
    //Etc...
}

else{

    [self.lbl1 removeFromSuperview];
}
Antonio MG
  • 20,382
  • 3
  • 43
  • 62
1

Try like this:

-(void)reloadData
if(!lbl1)
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)];

if (result1 > result2 &&  al == YES)
{
    lbl1.userInteractionEnabled = YES;
    lbl1.text = @" Warning!! ";
    lbl1.tag = 30;
    lbl1.font = [UIFont fontWithName:@"Helvetica" size:18.0];
    lbl1.textColor = [UIColor redColor];
    lbl1.backgroundColor = [UIColor clearColor];
    lbl1.lineBreakMode = NSLineBreakByWordWrapping;
    lbl1.numberOfLines = 2;
    [self addSubview:lbl1];
    [lbl1 release];
}

else{

    //Non of them is removing the label.
    [lbl1 removeFromSuperview];
    lbl1= nil;
    lbl1.hidden = YES;
}
Muthu Sabarinathan
  • 1,198
  • 2
  • 21
  • 49
0

Try to remove like this....

if (result1 > result2 &&  al == YES)
{
    lbl1.userInteractionEnabled = YES;
    lbl1.text = @" Warning!! ";
    lbl1.tag = 30;
    lbl1.font = [UIFont fontWithName:@"Helvetica" size:18.0];
    lbl1.textColor = [UIColor redColor];
    lbl1.backgroundColor = [UIColor clearColor];
    lbl1.lineBreakMode = NSLineBreakByWordWrapping;
    lbl1.numberOfLines = 2;
    [self addSubview:lbl1];
    [lbl1 release];
}

else{

    //Non of them is removing the label.
    [[self.view viewWithTag:30] removeFromSuperview];
    lbl1= nil;
    lbl1.hidden = YES;
}
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66