1

我正在按照教程进行基于 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
4

1 回答 1

-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;
}
于 2013-01-13T20:50:17.830 回答