我的视图控制器无法加载Game Center

时间:2013-01-12 14:51:32

标签: ios objective-c game-center

我正在按照教程进行游戏中心回合制比赛,井字游戏,我的视图控制器未加载。当我使用源代码但不能使用我自己键入的代码时,它可以工作。我检查过,无法找到任何区别。

我进入游戏中心,当我点击“播放你的回合”时,它会使用“开始游戏”按钮加载初始屏幕,而不是像原始代码那样加载“tictactoeGameViewController”。

如果有人能在这里帮助我,我将非常感激: - )

这是我的代码:

#import "ViewController.h"
#import "tictactoeGameViewController.h"

@interface ViewController ()

@end

@implementation ViewController


-(IBAction)beginGame:(id)sender {

GKMatchRequest *match = [[GKMatchRequest alloc]init];
[match setMaxPlayers:2];
[match setMinPlayers:2];

GKTurnBasedMatchmakerViewController *tmvc = nil;
tmvc = [[GKTurnBasedMatchmakerViewController alloc]initWithMatchRequest:match];
[tmvc setTurnBasedMatchmakerDelegate:self];
[self presentModalViewController:tmvc animated:YES];
[tmvc release];
[match release];

}



- (void)viewDidLoad {

[super viewDidLoad];

if ([GameCenterManager isGameCenterAvailable]) {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(localUserAuthenticationChanged:) name:GKPlayerAuthenticationDidChangeNotificationName object:nil];
    gcManager = [[GameCenterManager alloc]init];
    [gcManager setDelegate:self];
    [gcManager authenticateLocalUser];
}
}

-(void)processGameCenterAuthentication:(NSError *)error {
if (error != nil) {
    NSLog(@"An error occured during authentication: %@", [error localizedDescription]);
}
}

-(void)localUserAuthenticationChanged:(NSNotification *)notif {
NSLog(@"Authentication Changed: %@", notif.object);
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


#pragma mark - GCTurnBasedMatchHelperDelegate

-(void)turnBasedMatchmakerViewController:(GKTurnBasedMatchmakerViewController *)viewController didFindMatch:(GKTurnBasedMatch *)match {

[self dismissModalViewControllerAnimated:YES];

tictactoeGameViewController *gameVC = [[tictactoeGameViewController alloc]init];
gameVC.match = match;
[[self navigationController]pushViewController:gameVC animated:YES];
[gameVC release];

}

-(void)turnBasedMatchmakerViewController:(GKTurnBasedMatchmakerViewController *)viewController playerQuitForMatch:(GKTurnBasedMatch *)match {
[match participantQuitOutOfTurnWithOutcome:GKTurnBasedMatchOutcomeQuit withCompletionHandler:^(NSError *error) {
    if (error) {
        NSLog(@"An error occured ending match: %@", [error localizedDescription]);
    }
}];
}

-(void)turnBasedMatchmakerViewController:(GKTurnBasedMatchmakerViewController *)viewController didFailWithError:(NSError *)error {
NSLog(@"Turned Based Matchmaker Failed with Error: %@", [error localizedDescription]);
}

-(void)turnBasedMatchmakerViewControllerWasCancelled:(GKTurnBasedMatchmakerViewController *)viewController {
[self dismissModalViewControllerAnimated:YES];
}

@end

1 个答案:

答案 0 :(得分:-1)

我确实找到了解决方案:

ViewController必须是RootViewController,所以我改变了:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

要:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

ViewController *controller = [[ViewController alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}