如何删除动态创建的UIView和UILabel文本?

时间:2013-04-01 11:02:01

标签: ios uiview

我正在添加一些名为* cellSeparator和其他UILabels的动态UIView ...现在发生的事情是当我再次调用此代码然后重写标签文本并覆盖以前创建的标签文本...我不是很多意识到这个ios development.so任何人都可以告诉我如何在再次创建之前动态删除这个UIView?因为UIView是动态创建的我不知道如何删除该UIview

  UILabel *indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-150, self.view.frame.size.width/2,30)];
            [indexLabel setBackgroundColor:[UIColor clearColor]];
            indexLabel.textColor = [UIColor whiteColor];
            indexLabel.text = @"Details:-";
            indexLabel.font = [UIFont systemFontOfSize:20.00];
            UILabel *tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-120, self.view.frame.size.width/2, 30)];
            tagLabel.backgroundColor = [UIColor clearColor];


            NSLog(@"LOg %@",imageId);
            NSLog(@"LOg %@",imageStyle);
            NSLog(@"LOg %@",imageType);
            NSLog(@"LOg %@",imageWeight);
            tagLabel.text = [NSString stringWithFormat:@"The Id of Jewl Is:  %@",imageId];

            imageTypelabel= [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-90, self.view.frame.size.width/2, 30)];
            imageTypelabel.backgroundColor = [UIColor clearColor];
            imageTypelabel.text = [NSString stringWithFormat:@"The Type of Jewl Is:  %@",imageType];
            imageStylelabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-60, self.view.frame.size.width/2, 30)];
            imageTypelabel.backgroundColor = [UIColor clearColor];
            imageStylelabel.text = [NSString stringWithFormat:@"The style of Jewl Is:  %@",imageStyle];
            imageWeightlabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-30, self.view.frame.size.width/2, 30)];
            imageStylelabel.backgroundColor = [UIColor clearColor];
            imageWeightlabel.text = [NSString stringWithFormat:@"The weight of Jewl Is:  %@",imageWeight];
            imageWeightlabel.backgroundColor = [UIColor clearColor];
            imageWeightlabel.textColor = [UIColor whiteColor];
            imageTypelabel.textColor = [UIColor whiteColor];
            imageWeightlabel.textColor = [UIColor whiteColor];
            tagLabel.textColor = [UIColor whiteColor];
            UIImage *imageBegin = [UIImage imageNamed:imageName];
            UIImageView *imageView = [[UIImageView alloc] initWithImage:imageBegin];


            UIView *cellSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,545, self.view.frame.size.width ,3)];
            cellSeparator.tag=1;
            [cellSeparator setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
             UIViewAutoresizingFlexibleRightMargin | 
             UIViewAutoresizingFlexibleWidth];       
            [cellSeparator setContentMode:UIViewContentModeTopLeft];    
            [cellSeparator setBackgroundColor:[UIColor whiteColor]];
            [self.view addSubview:cellSeparator]; 

1 个答案:

答案 0 :(得分:7)

您可以编写一个方法来删除视图的所有子视图,并根据需要修改此代码。

- (void)removeSubviewsOfView
{
    NSArray *subViews = [self.view subviews];
    for(UIView *view in subViews)
    {
        [view removeFromSuperview];
    }
}