动态创建的标签被覆盖

4 iphone objective-c

我正在开发一个应用程序,其中我在一个函数中动态添加了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];
   }
Run Code Online (Sandbox Code Playgroud)

Sim*_*.IC 5

在for循环之前写下面的代码来获得您的要求:

for (id object in [subcatScroll subviews]) {

    [object removeFromSuperview];
}
Run Code Online (Sandbox Code Playgroud)