开发者

iphone : Label values over write.

I am inserting label in UIButton using addSubView

Here the label's value will be change every time.

    UILabel *backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];
    backTopLabel.backgroundColor = [UIColor clearColor];
    backTopLabel.textColor = [UIColor whiteColor];
    backTopL开发者_如何学Goabel.font = [UIFont fontWithName:@"Georgia" size:56];
    backTopLabel.textAlignment = UITextAlignmentCenter;

    backTopLabel.text = [[selectedUsers objectAtIndex:userIndex] valueForKey:@"FirstName"];   // UserIndex will be change and new data will be load in the label


    [btnBackLeftCard addSubview:backTopLabel];   // btnBackLeftCard is the UIButton
    [backTopLabel release];

The Problem is : It successfully changes the label value but it is over writing it.

I am getting the new label value over the old label value.

How can I solve this ?


Every time you call [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)] you are creating new memory, not accessing the existing memory.

So, you need to have something like

if(!backTopLabel)
    backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];

so that you only create the memory if it doesn't exist.

However since your label isn't a property, you're essentially losing access to it anyway. So you either need to add the label as a property of the class you're in OR tag the view so you can find it again & remove it.

Tag it & find again like this:

for(UIView* labelView in btnBackLeftCard.subviews)
{
   if(labelView.tag = 100)
      [labelView removeFromSuperView];
}

UILabel *backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];
backTopLabel.backgroundColor = [UIColor clearColor];
backTopLabel.textColor = [UIColor whiteColor];
backTopLabel.font = [UIFont fontWithName:@"Georgia" size:56];
backTopLabel.textAlignment = UITextAlignmentCenter;

//here is where you tag the view, so you can find it again
backTopLabel.view.tag = 100;

backTopLabel.text = [[selectedUsers objectAtIndex:userIndex] valueForKey:@"FirstName"];   

[btnBackLeftCard addSubview:backTopLabel];   // btnBackLeftCard is the UIButton
[backTopLabel release];


Assuming you have only one subview to be inserted in UIButton you can use

for(UILabel *lblViews in [btn subviews]) //Remove all subviews which are labels under button first if any
 {
      if(lblViews.tag == sometagno) //Compare tag number and remove only if that label found.
              [lblViews removeFromSuperview];
 }

backTopLabel.tag = sometagno; //Assign some tag number while adding.
[btnBackLeftCard addSubview:backTopLabel]; //Than Add your subview


This basically happens when a view gets added as subview on another view.. In your case it's been added multiple times.

Create one UILabel and then simple change it's text property.

In your case you're creating it multiple times and adding on top of the previous one.

Or if you cannot reuse previously created one, try removing it from the superview then add the latest one..


I think you are adding the subview (UILabel) again. YOu should only change the text of UILabel


I am working in COCOS2D. If you have declared labelname globally then use

[labelName removeFromParentAndCleanup:YES]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜