我正在尝试一个多视图应用程序,但是当我引入新的视图控制器时,我似乎无法让第一个视图控制器消失。我将第二个(即将到来的)视图控制器放在索引0处,它只是将它放在后台。我认为[going.view removeFromSuperview]会删除原来的viewcontroller,但那不是发生了什么......
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];
答案 0 :(得分:1)
首先进行一些重构:
coming = (answer ? boyController : girlController);
您可以删除going
和transition
,因为它们只使用过一次。然后,要实际执行动画,您需要将所有内容放在动画块的上下文中。
[UIView beginAnimations:@"flipAnimation" context:NULL];
[UIView setAnimationTransition:transition forView:self.view cache:YES];
[getInfoController.view removeFromSuperview];
[self.view addSubview:coming.view];
[UIView commitAnimations];
viewWillAppear:
和viewWillDisappear:
是委托方法。如果有的话,这些将在这些视图的代理上自动调用。不应该手动调用它们。