1

我在 Xcode 中创建了一个游戏,现在我决定给它添加一个菜单,菜单是第一个加载视图,所以我改变了

self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

到:

self.viewController = [[[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil] autorelease];

现在我的游戏所在的视图是: ViewController.m 并从我去那里的菜单中:

-(IBAction)gogame:(id)sender {
UIViewController *Game = [[UIViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Game animated:YES];}

因为我需要给我在 .h 和 .m 中更改的 ViewController.ma 名称:

@interface ViewController 到 .h 中的 @interface GameViewController

和@implementation ViewController 到.m 中的@implementation GameViewController

现在我让菜单“gogame”中的按钮运行它,当我单击该按钮时,它从菜单进入黑屏,它不会崩溃或任何东西它只显示状态栏和黑屏。xCode 给我的唯一问题是应用程序委托:从“MenuViewController *”分配给“GameViewController *”的指针类型不兼容。

我不知道为什么这不起作用我希望有人可以解释我并告诉我如何解决这个问题。谢谢

4

1 回答 1

1

单独的 UIViewController 没有什么用,它提供了添加行为,所以在你的方法中:

这是此方法的版本:

-(IBAction)gogame:(id)sender {
    UIViewController *Game = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:Game animated:YES];
}

它正在做你告诉它的事情,呈现一个框架提供的 UIViewController。您没有添加任何额外的行为或自定义视图。

我无法完全解释您当前的代码设置,但听起来您的新类 GameViewController 是您想要显示的,因此将其更改为:

-(IBAction)gogame:(id)sender {
    GameViewController *Game = [[[GameViewController alloc] initWithNibName:@"GameViewController" bundle:nil] autorelease];
    [self presentModalViewController:Game animated:YES];
}

根据您对类名的重命名/重构,我不确定控制器的 xib 文件的名称是什么。它是原始的“ViewController”还是您也将其更新为“GameViewController”(就像您应该的那样)?

关于警告,您在哪里创建和分配 GameViewController?

于 2012-06-08T23:04:01.107 回答