[self.view addSubView:WHATEVER]

Discussion in 'iOS Programming' started by bbrosemer, Apr 21, 2009.

  1. macrumors 6502a

    bbrosemer

    Joined:
    Jan 28, 2006
    #1
    Code:
    - (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 ...
     
  2. macrumors regular

    Joined:
    Jun 18, 2008
    #2
    int i,j;

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

    for (i = j; i >=0; i--)
    {
    [[[self.view subviews] objectAtIndex:i] removeFromSuperview];
    }
     
  3. thread starter macrumors 6502a

    bbrosemer

    Joined:
    Jan 28, 2006
    #3
    So self.view subviews removefromsyoerview can remove any generic subview?
     
  4. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #4
    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:

    Code:
    for (UIVIew *view in [self.view subviews]) {
    	[view removeFromSuperview];
    }
     
  5. thread starter macrumors 6502a

    bbrosemer

    Joined:
    Jan 28, 2006
    #5
    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
     

Share This Page