在视图控制器之间切换

发布于 2024-12-11 21:11:20 字数 2384 浏览 0 评论 0原文

我已经在应用商店上出售的几个应用程序上使用了这种方法,但出于某种原因,我正在开发的当前应用程序让我发疯......我一定是忽略了一些东西。

应用程序的主 viewController .h 文件:

#import "MainMenuController.h"
#import "GamePlay.h"

@interface ProjectNameiPhoneViewController : UIViewController <MenuDelegate, GameDelegate> {
      UIViewController *currentPageController;
}

应用程序启动并加载 MainMenu viewController:

UIViewController *nextController = [[MainMenuController alloc] initWithNibName:@"MainMenuController" bundle:nil];
[nextController performSelector:@selector(setDelegate:) withObject:self];

[self.view addSubview:nextController.view];

currentPageController = nextController;
[currentPageController retain];
[nextController release];

从 MainMenuController.m 中,用户选择开始游戏:强>

[delegate startGameplay:self];

返回应用程序的主视图控制器:

- (void)startGameplay:(MainMenuController *)sender {

    UIViewController *nextController = [[GamePlay alloc] initWithNibName:@"GamePlay" bundle:nil];

    [nextController performSelector:@selector(setDelegate:) withObject:self];

    [currentPageController.view removeFromSuperview];
    [self.view addSubview:nextController.view];

    [currentPageController release];
    currentPageController = nextController;
    [currentPageController retain];
    [nextController release];
}

在游戏屏幕上,用户点击后退按钮返回主菜单:

- (IBAction)backTapped {
    [delegate backToMenu:self];
}

返回应用程序的主视图控制器:

- (void)backToMenu:(GamePlay *)sender {

    UIViewController *nextController = [[MainMenuController alloc] initWithNibName:@"MainMenuController" bundle:nil];

    [nextController performSelector:@selector(setDelegate:) withObject:self];

    [currentPageController.view removeFromSuperview];
        [self.view addSubview:nextController.view];

    [currentPageController release];
    currentPageController = nextController;
    [currentPageController retain];
    [nextController release];   
}

我曾经再次选择从主菜单开始游戏。

GamePlay 类/Nib 加载,我再次单击后退按钮返回主菜单。

此时应用程序崩溃,没有信息打印到控制台。

任何想法将不胜感激 - 我已经注释掉了代码中的几乎所有其他内容,以至于 viewController 之间的切换实际上是这样的唯一正在运行的代码,我不知道为什么它会崩溃......

提前非常感谢您的帮助!

I've used this method on several apps that are for sale on the app store, but for some reason the current app I am working on is driving me nuts... I must be overlooking something.

The app's main viewController .h file:

#import "MainMenuController.h"
#import "GamePlay.h"

@interface ProjectNameiPhoneViewController : UIViewController <MenuDelegate, GameDelegate> {
      UIViewController *currentPageController;
}

The app starts up and loads the MainMenu viewController:

UIViewController *nextController = [[MainMenuController alloc] initWithNibName:@"MainMenuController" bundle:nil];
[nextController performSelector:@selector(setDelegate:) withObject:self];

[self.view addSubview:nextController.view];

currentPageController = nextController;
[currentPageController retain];
[nextController release];

From MainMenuController.m, the user chooses to start the game:

[delegate startGameplay:self];

Back in the app's main viewController:

- (void)startGameplay:(MainMenuController *)sender {

    UIViewController *nextController = [[GamePlay alloc] initWithNibName:@"GamePlay" bundle:nil];

    [nextController performSelector:@selector(setDelegate:) withObject:self];

    [currentPageController.view removeFromSuperview];
    [self.view addSubview:nextController.view];

    [currentPageController release];
    currentPageController = nextController;
    [currentPageController retain];
    [nextController release];
}

From the gameplay screen, user hits the back button to return to the main menu:

- (IBAction)backTapped {
    [delegate backToMenu:self];
}

Back in the app's main viewController:

- (void)backToMenu:(GamePlay *)sender {

    UIViewController *nextController = [[MainMenuController alloc] initWithNibName:@"MainMenuController" bundle:nil];

    [nextController performSelector:@selector(setDelegate:) withObject:self];

    [currentPageController.view removeFromSuperview];
        [self.view addSubview:nextController.view];

    [currentPageController release];
    currentPageController = nextController;
    [currentPageController retain];
    [nextController release];   
}

I once again choose to start the game from the main menu.

The GamePlay class/Nib loads, and I once again click the back button to return to the main menu.

At this point the app crashes, with no information printed to the console.

Any ideas would be GREATLY appreciated - I've commented out almost everything else in my code to the point where this switching between viewControllers is practically the only code being run and I'm at a loss as to why it is crashing...

Thanks so much in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人骨 2024-12-18 21:11:20

您可能希望将 MainMenuController 作为应用程序 keyWindow 的 rootViewController。

此外,UIViewController 的视图不应添加到其他 UIViewController 的视图中,因为这会破坏响应者链,并且在 iOS 5.0 中会引发异常。如果您选择仅将其实现为 iOS 5.0 应用程序,那么我建议您查看 [UIViewController -addChildViewController:][UIViewController -removeChildViewController:]

中的说明,如果可行的话,更好的解决方案是将 MainMenuController 作为 rootViewController ,如上所述,然后调用 [UIViewController -presentModalViewController:animated:]您的 startGameplay 方法并使用委托来关闭模态视图控制器

It's likely that you want to have the MainMenuController as the rootViewController of your application's keyWindow.

Additionally, UIViewController's views should not be added to other UIViewController's views as this breaks the responder chain and in iOS 5.0, will throw an exception. If you choose to only implement it as an iOS 5.0 application, then I recommend taking a look at [UIViewController -addChildViewController:] and [UIViewController -removeChildViewController:]

In saying this, a better solution if doable, would be to have the MainMenuController as the rootViewController as stated above, and then to call [UIViewController -presentModalViewController:animated:] in your startGameplay method and use the delegate to dismiss the modal view controller

平生欢 2024-12-18 21:11:20

这似乎不是一个真正的解决方案,但应用程序只会在我通过 Xcode 的“构建和运行”运行它时崩溃。如果我通过单击设备本身上的图标打开应用程序,则该应用程序可以完美运行。我可以长时间玩它,并快速地按下按钮在视图控制器之间切换,而且它不会崩溃。因此,我将向 App Store 提交它,尽管它每次在通过 Xcode 运行时都会崩溃而不会出现错误消息 - 它绝对不会在设备本身上崩溃。

通过 Xcode 运行时崩溃的最奇怪的部分是:
应用程序在设备上崩溃,退出到主屏幕。但是,Xcode 仍然将应用程序显示为“正在运行”,并且我可以选择单击“停止”——它不会变灰。正如我所说,没有错误消息打印到控制台。

This doesn't seem like a true resolution but the app only crashes when I'm running it through Xcode's "build and run." If I open the app by clicking the icon on the device itself, the app runs perfectly. I can play it for long periods of time and mash the buttons to switch between view controllers over and over again very quickly, and it does not crash. So I'm going to submit it for the App Store even though it crashes every time without an error message when running through Xcode - it absolutely does not crash on the device itself.

The strangest part about the crash when running through Xcode is this:
The app crashes on the device, exiting out to the home screen. However, Xcode still shows the app as "Running" and I have the option to click "Stop" - it is not grayed out. As I said, no error message is printed to the console.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文