简体   繁体   中英

How to remove the nsmutablearray's object from subview?

i have nsmutablearray in which i saved some character means alphabets now what i want is to display these objects as subview on screen. and i did it easily..and em doing this through loop.. i am accessing arrays object one by one by which i can add these as subview but now em unable to remove these from super view. I want to remove these labels. how can i do that?

UILabel *myLabel;
UIImageView *image;

for (int j = 0; j<[intValues count];j++) {
   image = [allGridImages objectAtIndex:j];
    image.userInteractionEnabled=NO;
    image.multipleTouchEnabled=NO;
    image.image= [UIImage imageNamed:@"box-1.png"];

    title = [allGridBoxesTitle objectAtIndex:j];
    myLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 1, 45, 45)];
    myLabel.text = [allGridBoxesTitle objectAtIndex:j];
    myLabel.textColor = [UIColor grayColor];
    myLabel.font = [UIFont boldSystemFontOfSize:26.0f];
    myLabel.backgroundColor = [UIColor clearColor];
    [image addSubview:myLabel];

Try this method

- (void)removeAllLabels
{
    for (UIView *view in self.view.subviews)
    {
        if ([view isKindOfClass:[UILabel class]])
        {
            [view removeFromSuperview];
        }
    }
}

You could do this before adding the new label:

[image.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

This will instruct any subviews to remove themselves.

However, this depends on the fact that a UIImageView doesn't have any subviews in the current version of iOS, which may not always be true. It'd be better to actually keep track of these labels and specifically remove them when you're finished with them, or even reuse them instead of making new ones every time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM