tags:

views:

1152

answers:

2

I have a UIViewController class where I create a UIView instance. And then I initialize (means fill) it with 25 subviews each containing an image (1, 2, ..., 25). Then after clicking 5 times in these image I called a function where I used

for(UIView *subview in [contentView subviews]) {
    [subview removeFromSuperview];//ContentView name of my view
}

to remove the previously added subview. And then I use the same approch to add 25 new subviews (image 1,2,3,....25). But this time no subview is added. Can someone plz give me full code of adding & removing subview.

I have used the following code when I first add subview

//create main window

contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blackColor];
self.view = contentView;
[contentView release];

//adding 25 subview 1st time
int a=0;
int b=0;
for (int i = 0; i < 26; i++)
{

    CGRect dragRect = CGRectMake(0.0f, 0.0f, x, y);
    dragRect.origin = CGPointMake(a,b);
    DragView *dragger = [[DragView alloc] initWithFrame:dragRect];
    NSString *Flower = [[NSArray arrayWithObjects:@"1.png", @"2.png", @"3.png",@"4.png", @"5.png", @"6.png",@"7.png",@"8.png", @"9.png",@"10.png", @"11.png", @"12.png",@"13.png",@"14.png",@"15.png",@"16.png",@"17.png",@"18.png",@"19.png",@"20.png",@"21.png",@"22.png",@"23.png",@"24.png",@"25.png",@"26.png", nil] objectAtIndex:i];
    [dragger setImage:[UIImage imageNamed:Flower]];
    [dragger setUserInteractionEnabled:YES];
    [self.view addSubview:dragger];
    [dragger release];
    a+=10;
    b+=10;
}

//then removing 25 subview

//adding 25 subview 2nd times

I used the same approch to add the second time as first time, but the problem is that when I remove 25 subview and then add 25 subview, these subview are not added/shown, the view remain same. I am tired with these problem. plz someone help me.

A: 

Looking at your code, I can suggest the following changes. There's one line in your for loop which is terribly inefficient:

NSString *Flower = [[NSArray arrayWithObjects:@"1.png", @"2.png", @"3.png",@"4.png", @"5.png", @"6.png",@"7.png",@"8.png", @"9.png",@"10.png", @"11.png", @"12.png",@"13.png",@"14.png",@"15.png",@"16.png",@"17.png",@"18.png",@"19.png",@"20.png",@"21.png",@"22.png",@"23.png",@"24.png",@"25.png",@"26.png", nil] objectAtIndex:i];
[dragger setImage:[UIImage imageNamed:Flower]];

Either take the Flower initialisation out of the the for loop (to only create the array once) or do the following:

[dragger setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i]]];

The code itself looks like it should work though. If you add 26 subviews then remove the 26 subviews then add the 26 subviews in exactly the same way then it should display as you'd expect.

Nick Bedford
+1  A: 

The problem might be the way you remove the old views. You are modifying an array while iterating over it, which does not work. Try this code to remove the old views:

UIView* subview;
while ((subview = [[contentView subviews] lastObject]) != nil)
    [subview removeFromSuperview];

BTW: You asked 9 questions without accepting any single answer. This is not very encouraging for people to take time to write a thoughtful answer.

Nikolai Ruhe