1

次のデリゲート メソッドは、人が招待を受け入れると、gameKit によって呼び出されます。

-(void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite
{
    NSLog(@"%@ accepted invite",player.playerID);

    RIYGameNavigationController *root = (RIYGameNavigationController*)[[[UIApplication sharedApplication]delegate]window].rootViewController;
    [root popToRootViewControllerAnimated:YES];
    GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc]initWithInvite:invite];
    mmvc.matchmakerDelegate = root.viewControllers[0];
    [[[[[UIApplication sharedApplication]delegate]window]rootViewController]presentViewController:mmvc animated:YES completion:nil];
}

これは、ユーザーがまだGKMatchmakerViewController.

招待が届いたときにユーザーがすでに GKMatchmakerViewController を使用している場合、これは機能しません (例: ユーザーが既に一致を見つけようとしている場合)。この場合、招待が届いたときに次の警告が表示され、招待されたコントローラーは表示されません (表示されている matchmakercontroller が既に存在するため)。その結果、ユーザーは招待に応答できません。(何も起こりません)。

Warning: Attempt to present <GKMatchmakerViewController: 0x1458a250>  on <RIYGameNavigationController: 0x1461f190> which is already presenting <GKMatchmakerViewController: 0x1465fcb0>

iOS6 では、次のようにしてこれを回避できます。

 if([topLevelViewController modalViewController] != nil)      
[topLevelViewController dismissModalViewControllerAnimated:NO];

を作成しGKMatchmakerViewControllerます。dismissModalViewControllerAnimated:iOS6 で廃止されました。代わりに使用することをお勧めしますdissmissViewControllerAnimated:completion:が、私にはうまくいきません。これは私が試したものです:

-(void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite
{
    NSLog(@"%@ accepted invite",player.playerID);

    RIYGameNavigationController *root = (RIYGameNavigationController*)[[[UIApplication sharedApplication]delegate]window].rootViewController;
    [root popToRootViewControllerAnimated:YES];
    if ([[root presentedViewController]class] == [GKMatchmakerViewController class]) {
        [root dismissViewControllerAnimated:YES completion:nil];
    }

    GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc]initWithInvite:invite];
    mmvc.matchmakerDelegate = root.viewControllers[0];
    [[[[[UIApplication sharedApplication]delegate]window]rootViewController]presentViewController:mmvc animated:YES completion:nil];
}

if ブロックに入りますが、既存の GKMatchmakerViewController は閉じられず、新しいものは表示されません。if ステートメントにブレーク ポイントを配置すると、ブレーク後に「続行」すると、ビュー コントローラーは閉じられますが、新しいビュー コントローラーはポップアップしません。

どうすればこれを機能させることができますか?

4

1 に答える 1

0

完了ブロックでビューを提示することで、これを回避しました。

- (void)presentMatchmakerViewControllerWithInvite:(GKInvite *)invite
{
    RIYGameNavigationController *root = (RIYGameNavigationController*)[[[UIApplication sharedApplication]delegate]window].rootViewController;
    GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc]initWithInvite:invite];
    mmvc.matchmakerDelegate = root.viewControllers[0];
    [[[[[UIApplication sharedApplication]delegate]window]rootViewController]presentViewController:mmvc animated:YES completion:nil];
}

-(void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite
{
     NSLog(@"%@ accepted invite",player.playerID);
     RIYGameNavigationController *root = (RIYGameNavigationController*)[[[UIApplication sharedApplication]delegate]window].rootViewController;
    [root popToRootViewControllerAnimated:YES];
    if ([[root presentedViewController]class] == [GKMatchmakerViewController class]) {
        [root dismissViewControllerAnimated:YES completion:^{
            [self presentMatchmakerViewControllerWithInvite:invite];
        }];
    }
    else
    {
        [self presentMatchmakerViewControllerWithInvite:invite];
    }
}

機能しているように見えますが、ぎこちなく感じます。誰かがより良い答えを持っている場合、私はそれを正しいものとしてマークします

于 2013-11-20T13:14:27.970 に答える