2011-05-02 57 views
0

我有一个有4个视图的应用程序; HomeView,PreGameView,GameView,PostGameView。该应用程序的工作方式,我开始在HomeView上,然后点击开始,这将我带到PreGameView,单击开始,这将我带到GameView,在结束游戏,它将我带到PostGameView,从中我可以回去到GameView或返回到HomeView。正确使用presentModalViewController?帮帮我?

我通过使用“presentModalViewController”方法对这些视图进行分层。但是,当我一直下到PostGameView,然后决定返回到HomeView时,屏幕变白。

我也尝试过在推出下一个模型之前解雇每个模型视图,但那不起作用,应用程序变得混乱。有谁知道这样做的正确方法?请帮忙?

HomeView.m

- (IBAction)loadPreGameView:(id)sender { 
    PreGameView *preGameView = [[PreGameView alloc] initWithNibName:nil bundle:nil]; 
    [self presentModalViewController:preGameView animated:YES]; 
} 

PreGameView.m

- (IBAction)loadGameView:(id)sender { 
    GameView *gameView = [[GameView alloc] initWithNibName:nil bundle:nil]; 
    [self presentModalViewController:gameView animated:NO]; 
} 

GameView.m

- (IBAction)loadPostGameView:(id)sender { 
    PostGameView *postGameView = [[PostGameView alloc] initWithNibName:nil bundle:nil]; 
    [self presentModalViewController:postGameView animated:NO]; 
} 

PostGameView.m

- (IBAction)loadGameView:(id)sender { 
    GameView *gameView = [[GameView alloc] initWithNibName:nil bundle:nil]; 
    [self presentModalViewController:gameView animated:NO]; 
} 

- (IBAction)loadHomeView:(id)sender { 
    HomeView *homeView = [[HomeView alloc] initWithNibName:nil bundle:nil]; 
    [self presentModalViewController:homeView animated:NO];  
} 

我也试过下面我PostGameView.m,但它只是表明它背后的GameView:

PostGameView.m(旧)

- (IBAction)loadGameView:(id)sender { 
    GameView *gameView = [[GameView alloc] initWithNibName:nil bundle:nil]; 
    [self presentModalViewController:gameView animated:NO]; 
} 

- (IBAction)loadHomeView:(id)sender { 
    [self dismissModalViewControllerAnimated:YES]; 
} 

回答

1

更好的方法是使用UINavigationController来做到这一点。

创建一个UINavigationController时,init与HomeView 从那里...... [self.navigationController pushViewController:nextViewController动画:NO]

当你完成,要弹出的开始[self.navigationController popToRootViewControllerAnimated:NO ]。

关于使用NavController的好处是,每个UIViewController都会通过调用self.navigationController自动引用持有它的NavController;

如果你想全屏,你也可以关闭导航条。

另外,UINavigationController保留了它的ViewControllers,所以你应该在你“推”它之后调用你创建的任何UIViewController的release。

0

你有一个视图控制器链。您应该在每个级别解雇以回到父母身边。你也可以上升到多个级别并解雇,并且会弹出下面的所有孩子。您可能还有一个memleak,因为您应该在分配视图控制器或明确释放视图控制器后自动释放视图控制器。

+0

但是当我做'[self dismissModalViewControllerAnimated:NO]; GameView * gameView = [[GameView alloc] initWithNibName:nil bundle:nil]; [self presentModalViewController:gameView animated:NO];'在PreGameView.m中 - 视图只是从PreGameView回到HomeView,而不是GameView。 – buzzkip 2011-05-02 18:24:31