gpt4 book ai didi

ios - 来自另一个 View Controller 时的 xcode 问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:36:05 24 4
gpt4 key购买 nike

我正在开发 iPhone 应用程序,遇到了一些问题。我有一个主视图 Controller 和另一个名为 GameViewController 的 View Controller 。我在主视图 Controller 中有一个按钮可以转到游戏,在游戏中有一个后退按钮可以返回主视图 Controller 。如果我转到游戏 View Controller 并返回主界面,下次我按下按钮转到游戏 View Controller 时,我会发现各种故障。这是我用于转到 gameViewController 的按钮的代码:

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

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

这是转到主视图 Controller 的后退按钮代码:

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

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

最佳答案

您有没有使用 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];

您只在一个地方实例化“home”和“game” View Controller ,UINavigationController 在其堆栈的顶部显示 View Controller 。

关于ios - 来自另一个 View Controller 时的 xcode 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22119913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com