2009-04-10 59 views
1

我正在嘗試一個多視圖應用程序,但我似乎無法得到第一個視圖控制器消失,當我把新的視圖控制器。我在索引0處放置第二個(即將到來的)視圖控制器,並將它放置在背景中。我認爲[going.view removeFromSuperview]將刪除原視圖 - 控制,但是這不是發生了什麼......讓一個視圖控制器消失

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

首先一點重構:

coming = (answer ? boyController : girlController); 

可以刪除goingtransition,因爲它們只用過一次。然後,要實際執行動畫,您需要將所有內容放入動畫塊的上下文中。

[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:是委託方法。這些將在這些視圖的代表中自動調用,如果有的話。他們不應該被手動調用。

相關問題