Iphone 动态创建的标签将被覆盖

Iphone 动态创建的标签将被覆盖,iphone,objective-c,Iphone,Objective C,我正在处理一个应用程序,其中我在一个函数中动态添加了5个标签。当我调用同一个函数时,尽管每次创建时都会释放标签,但标签会覆盖以前创建的标签 for(int i = 1; i < [array count]; i++) { CGRect lblframe = CGRectMake(count, ycord, width, height); UILabel *label = [[UILabel alloc] initWithFrame:lb

我正在处理一个应用程序,其中我在一个函数中动态添加了5个标签。当我调用同一个函数时,尽管每次创建时都会释放标签,但标签会覆盖以前创建的标签

    for(int i = 1; i < [array count]; i++)
    {   
        CGRect lblframe = CGRectMake(count, ycord, width, height);
        UILabel *label = [[UILabel alloc] initWithFrame:lblframe];
        label.backgroundColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:17];
        label.textAlignment = UITextAlignmentCenter;
        label.textColor = [UIColor colorWithRed:(188/255.f) green:(149/255.f) blue:(88/255.f) alpha:1.0];;
        label.text = [arr objectAtIndex:i];

        count = count + xcord;
        [subcatScroll addSubview:label];           
        [label release];
   }
for(int i=1;i<[数组计数];i++)
{   
CGRect lblframe=CGRectMake(计数、ycord、宽度、高度);
UILabel*label=[[UILabel alloc]initWithFrame:lblframe];
label.backgroundColor=[UIColor blackColor];
label.font=[UIFont systemFontOfSize:17];
label.textAlignment=UITextAlignmentCenter;
label.textColor=[UIColor colorWithRed:(188/255.f)green:(149/255.f)blue:(88/255.f)alpha:1.0];;
label.text=[arr objectAtIndex:i];
计数=计数+xcord;
[子目录添加子视图:标签];
[标签发布];
}

在for循环之前编写以下代码,以满足您的需求:

for (id object in [subcatScroll subviews]) {

    [object removeFromSuperview];
}

我不确定我是否完全理解,所以如果我有误解,请纠正我。 每次调用此函数时,都会添加许多新标签。因此,如果您第二次调用此函数,假设“count”、“ycord”、“width”和“height”与第一次调用的值相对应,那么很明显,您正在将第二组标签添加到与第一组标签相同的位置,这些标签现在直接位于另一组标签之上。您没有“覆盖”旧标签,而是将第二个组直接放置在旧标签的顶部

在每个标签上调用“release”,只意味着将重新计数减少1。此号码仅用于内存管理。这意味着,如果现在从视图中删除标签,则会释放内存

CGRect lblframe = CGRectMake(10.0, ycord, 200.0, 20.0);
UILabel *label = [[UILabel alloc] initWithFrame:lblframe];
NSLog(@"retainCount of label: %d", [label reatinCount]); // will print "1" since you called alloc
[self.view addSubview:label];
NSLog(@"retainCount of label: %d", [label reatinCount]); // will print "2" since adding to subview increases retaincount by one
[label release];
NSLog(@"retainCount of label: %d", [label reatinCount]); // will print "1" since you released
[label removeFromSuperview]; // will decrease retainCount of label to "0" and therefore free the memory
因此,如果要从视图中删除以前创建的标签,则必须这样做。或者保留对它们的引用,并对每一个调用“removeFromSuperview”

如果视图中唯一要添加标签的对象,也可以删除添加到其中的每个子视图,如下所示:

// remove old labels
for (UILabel *aLabel in [self.view subviews]) [aLabel removeFromSuperview];

NSArray *myArray = [NSArray arrayWithObjects:@"I", @"II", @"III", @"IV", nil];
for (int i=0; i<[myArray count]; i++) {
    float ycord = i*40.0;
    CGRect lblframe = CGRectMake(10.0, ycord, 200.0, 20.0);
    UILabel *label = [[UILabel alloc] initWithFrame:lblframe];
    label.backgroundColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:17];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor colorWithRed:(188/255.f) green:(149/255.f) blue:(88/255.f) alpha:1.0];;
    label.text = [myArray objectAtIndex:i];

    [self.view addSubview:label];
    [label release];
}
//删除旧标签
对于[self.view子视图][aLabel removeFromSuperview]中的(UILabel*aLabel);
NSArray*myArray=[NSArray阵列,其对象为:@“I”、“II”、“III”、“IV”、“nil];

对于(int i=0;i)“当我调用同一个函数时,尽管每次创建时都会释放标签,但标签仍会覆盖先前创建的标签”,你能解释一下你的要求吗?我在scrollview中有一些动态创建的标签。当我单击标签的一个值(比如水平滚动中的第一个值)时,另一组值会显示在动态创建的标签中,就在上滚动视图的下方。现在,当我单击主滚动视图的第二个值时,则显示在下滚动视图被新值覆盖。我试图释放以前的标签,但问题没有得到解决。你真的需要创建新标签吗?你不能只替换现有标签的文本吗?谢谢你的支持,但现在我得到了(UILabel*aLabel in[self.view子视图][aLabel removeFromSuperview]的解决方案是的,很有效,谢谢简。