objective-cuiviewcontrollerios5imagenamed

UIImage that won't disappear when removing it from its superview


Under iOS 5, I encounter the following problem : the following code is called on a controller everytime a new product is picked in the application. It displays a bullet list of features in the controller's view.

Every bullet is an image that is added at the right place (in front of the corresponding text).

Now, the problem is that sometimes, apparently randomly, some bullets are not removed from the previous bullet lists, altough I remove it from its superview at the beginning of the methods.

  - (void) loadProduct{
    for (UIView * bullet in bullets){
        bullet.hidden = YES;
        [bullet removeFromSuperview];
    }
    [bullets removeAllObjects];
    [self.view setNeedsDisplay];


   ......here some stuff ......


    for (int i=0;i< numberOfFeatures;i++)
    {   

            UIImageView * bullet = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bullet.png"]];
            bullet.frame =CGRectMake(someFloatValue ,
                                     someOtherFloatValue,
                                     6,
                                     6);
            [self.view addSubview:bullet];

            [bullets addObject:bullet];
            [bullet release];

        }
    }

bullets is an NSMutableArray, a property of the controller.

Am I doing something wrong here?


Solution

  • In bullets it seems that you're only storing the labels:

    [bullets addObject:label];
    

    So, when removing everything from bullets, you're only removing the labels from the superview, the bullets (UIImageView controls) are left there.

    It might be easier if you create a custom control (a view holding both the bullet and the label) and use those instead