来自另一个视图控制器的xcode问题

时间:2014-03-01 20:25:45

标签: ios iphone uiviewcontroller

我正在开发iPhone应用程序并遇到了一些问题。我有一个主视图控制器和另一个名为GameViewController的控制器。我在主视图控制器中有一个按钮可以进入游戏,然后在游戏中返回一个按钮返回主视图控制器。如果我去游戏视图控制器并返回主,下次我按下按钮去游戏视图控制器,我发现各种故障。这是我转到gameViewController的按钮的代码:

   GameViewController *game = [[GameViewController alloc] initWithNibName:nil bundle:Nil];
    [self dismissViewControllerAnimated:YES completion:NULL];

[self presentViewController:game animated:YES completion:NULL];

以下是转到主视图控制器的后退按钮代码:

    ViewController *home = [[ViewController alloc] initWithNibName:nil bundle:nil];
UIViewController *parentViewController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^
 {
     [parentViewController presentViewController:home animated:NO completion:nil];
 }];

如果有人能提供帮助,我们将不胜感激。

3 个答案:

答案 0 :(得分:1)

有没有理由不使用UINavigationController?即使您想隐藏导航栏并提供自己的导航UI,也可以使用此功能。听起来你有自己的后退按钮就足够了。

使用UINavigationController,您的代码看起来更像是这样:

// in your application delegate's application:didFinishLaunchingWithOptions: method

UINavigationController *nav = [UINavigationController alloc] initWithRootViewController:home];
nav.navigationBarHidden = YES; // this hides the nav bar
self.window.rootViewController = nav;

// from your 'home' VC to present the game vc:

GameViewController *game = [[GameViewController alloc] initWithNibName:nil bundle:Nil];
[self.navigationController pushViewController:game animated:YES];

// from your 'game' VC to get back to 'home':

[self.navigationController popViewControllerAnimated:YES];

您只是在一个地方实例化“家庭”和“游戏”视图控制器,UINavigationController将视图控制器呈现在其堆栈的顶部。

答案 1 :(得分:0)

你'回'代码并没有真正回归,它会回来然后呈现一个新的家庭视图控制器副本。

然后,您的“游戏”代码将被解雇,这将删除当前的控制器(因为您在返回时呈现了它),然后从中呈现游戏。

这很麻烦,因为你会看到控制器的显示控制器已被破坏。你最好使用导航控制器。但是,如果您只是出现并解雇,则可以更正当前的代码:

主页 - >游戏:

GameViewController *game = [[GameViewController alloc] initWithNibName:nil bundle:Nil];
[self presentViewController:game animated:YES completion:NULL];

游戏 - >家

[self dismissViewControllerAnimated:YES completion:NULL];

答案 2 :(得分:0)

你应该寻找Segues!您无法关闭源viewcontroler。

相关问题