PDA

View Full Version : [self.view addSubView:WHATEVER]




bbrosemer
Apr 22, 2009, 01:20 AM
- (void)viewWillAppear:(BOOL)animated {

int i = 0;
int y = [xValues count];
for(i; i < y; i++)
{
UIImageView *tempView;
int xHolder = [[xValues objectAtIndex:i]intValue];
int yHolder = [[yValues objectAtIndex:i]intValue];
if((i+5)>=y)
{
tempView = [self newPieceViewWithImageNamed:@"newHits.png" atPostion:CGPointMake((xHolder),(yHolder))];
}
else{
tempView = [self newPieceViewWithImageNamed:@"Baseball.png" atPostion:CGPointMake((xHolder),(yHolder))];
tempView.alpha = .30;
}
[self.view addSubview:tempView];
}

firstPieceView = [self newPieceViewWithImageNamed:@"Baseball.png" atPostion:CGPointMake(290.,380)];
[self.view addSubview:firstPieceView];
}

Is there anyway when I leave the view to release all instances of the subviews ... there should be one but I don't know of what to call if I basically just want to start the view over with a clean slate the next time I call it ...



ghayenga
Apr 22, 2009, 08:21 AM
- (void)viewWillAppear:(BOOL)animated {

int i = 0;
int y = [xValues count];
for(i; i < y; i++)
{
UIImageView *tempView;
int xHolder = [[xValues objectAtIndex:i]intValue];
int yHolder = [[yValues objectAtIndex:i]intValue];
if((i+5)>=y)
{
tempView = [self newPieceViewWithImageNamed:@"newHits.png" atPostion:CGPointMake((xHolder),(yHolder))];
}
else{
tempView = [self newPieceViewWithImageNamed:@"Baseball.png" atPostion:CGPointMake((xHolder),(yHolder))];
tempView.alpha = .30;
}
[self.view addSubview:tempView];
}

firstPieceView = [self newPieceViewWithImageNamed:@"Baseball.png" atPostion:CGPointMake(290.,380)];
[self.view addSubview:firstPieceView];
}

Is there anyway when I leave the view to release all instances of the subviews ... there should be one but I don't know of what to call if I basically just want to start the view over with a clean slate the next time I call it ...

int i,j;

j = [[self.view subviews] count] - 1;

for (i = j; i >=0; i--)
{
[[[self.view subviews] objectAtIndex:i] removeFromSuperview];
}

bbrosemer
Apr 22, 2009, 09:57 AM
int i,j;

j = [[self.view subviews] count] - 1;

for (i = j; i >=0; i--)
{
[[[self.view subviews] objectAtIndex:i] removeFromSuperview];
}

So self.view subviews removefromsyoerview can remove any generic subview?

jnic
Apr 22, 2009, 10:09 AM
So self.view subviews removefromsyoerview can remove any generic subview?

Yup. "subviews" returns an array of the subviews for a given view. ghayenga's code snippet iterates through all of these subviews and removes them from the view. Here's some shorthand that does the same:

for (UIVIew *view in [self.view subviews]) {
[view removeFromSuperview];
}

bbrosemer
Apr 22, 2009, 12:16 PM
So basically you 2 are my heros ... I was thinking apple really needed a way to remove all subviews ... and then I get my answer that subviews returns an array of subviews .... that is beautiful