views:

249

answers:

1

I'm trying out a multiple view application, but I can't seem to get the first view controller to go away when I bring in the new view controller. I'm laying the second (coming) view controller at index 0, and it's just placing it in the background. I thought the [going.view removeFromSuperview] would remove the original viewcontroller, but that's not what is happening...

UIViewController *coming = nil;
    UIViewController *going = nil;
    UIViewAnimationTransition transition;

    if (answer == YES)
    {
     coming = boyController;
     going = getInfoController;
     transition = UIViewAnimationTransitionFlipFromLeft;
    }
    else
    {
     coming = girlController;
     going = getInfoController;
     transition = UIViewAnimationTransitionFlipFromLeft;
    }
    NSLog(child);
    [UIView setAnimationTransition:transition forView: self.view cache:YES];
    [coming viewWillAppear:YES];
    [going viewWillDisappear:YES];
    [going.view removeFromSuperview];
    [self.view insertSubview:coming.view atIndex:0];
    [going viewDidDisappear:YES];
    [coming viewDidAppear:YES];

    [UIView commitAnimations];
+1  A: 

First a little refactoring:

coming = (answer ? boyController : girlController);

You can delete going and transition, as they're only used once. Then, to actually do the animation, you need to put everything in the context of an animation block.

[UIView beginAnimations:@"flipAnimation" context:NULL];
[UIView setAnimationTransition:transition forView:self.view cache:YES];
[getInfoController.view removeFromSuperview];
[self.view addSubview:coming.view];
[UIView commitAnimations];

viewWillAppear: and viewWillDisappear: are delegate methods. These will be called automatically on those views' delegates, if any. They shouldn't ever be called manually.

Alex