Crystal
Crystal

Reputation: 29458

Removing superview in objective-c

I'm looking through an example in the iPhone Beginning programming book and they have code to switch between two views when a button is pressed. Here's the first snippet from their example code:

if (self.yellowViewController.view.superview == nil)
{
    if (self.yellowViewController == nil)
    {
        YellowViewController *yellowController =
        [[YellowViewController alloc] initWithNibName:@"YellowView"
bundle:nil];
        self.yellowViewController = yellowController;
        [yellowController release];
    }
    [blueViewController.view removeFromSuperview];
    [self.view insertSubview:yellowViewController.view atIndex:0];
}
else
{
    if (self.blueViewController == nil)
    {
        BlueViewController *blueController =
        [[BlueViewController alloc] initWithNibName:@"BlueView"
bundle:nil];
        self.blueViewController = blueController;
        [blueController release];
    }
    [yellowViewController.view removeFromSuperview];
    [self.view insertSubview:blueViewController.view atIndex:0];
}

It does make sense to me, but the question I have is, how would you do this with a UISegmentControl that has four views. I know you can check for the selectedSegment and create that view when needed. But how would I know what the last view was in order to remove it from the superview beforing adding my new view as a subview? Thanks!

Upvotes: 0

Views: 720

Answers (3)

SushiGrass Jacob
SushiGrass Jacob

Reputation: 19814

You could check to see which view is allocated or not nil and then remove.

 if (yellowController) {

 [yellowController.view removeFromSuperView];
 [yellowController release];

 }

You could go through your four views to determine which one is loaded and then remove the view.

Upvotes: 0

user756245
user756245

Reputation:

For any UIView the frontmost subview is [[myView subviews] lastObject].

Upvotes: 0

while creating each view either code or IB set tag value to segmentIndex.so u can get them later by that tag value.this is tricky and simple.

Upvotes: 1

Related Questions