获得玩家参与的回合制游戏

时间:2013-04-30 01:39:55

标签: iphone ios objective-c ios6 game-center

我正在尝试拉动玩家参与的回合制游戏,以便填充我的tableView。

这是我的游戏功能:

- (void) loadMatchDataWithArray:(NSMutableArray*)currentGames Flag:(bool*)returned
{
    NSMutableArray* __block blockGames = currentGames;
    bool* __block blockReturn = returned;

    [GKTurnBasedMatch loadMatchesWithCompletionHandler:^(NSArray *matches, NSError *error)
     {
         if (matches)
         {
             for (int i = 0; i < matches.count; i++)
             {
                 [(GKTurnBasedMatch*)matches[i] loadMatchDataWithCompletionHandler: ^(NSData *matchData, NSError *error)
                  {
                      int size = [matchData length];
                      if (size != 0)
                      {
                          Game* game = [NSKeyedUnarchiver unarchiveObjectWithData:matchData];
                          [blockGames addObject:game];
                      }
                      else
                      {
                          Game* game = [[Game alloc] init];
                          [blockGames addObject:game];
                          game.activePlayer = [GKLocalPlayer localPlayer];
                      }
                      *blockReturn = true;
                  }];
             }
         }
         else
         {
             *blockReturn = true;
         }
     }];
}

这就是我所说的:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[self tableView]
    setBackgroundView:[[UIImageView alloc]
    initWithImage:[UIImage imageNamed:@"iPhoneBackground-568h"]]];

    bool* returned = false;
    [[GKMatchHelper sharedInstance] loadMatchDataWithArray:currentGames Flag:returned];
    while (!returned);
    [self.tableView reloadData];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

可悲的是,这只是给我一个空白的黑屏,永远不会回来。是否有一种方法可以检测到我的块何时返回并显示一个加载微调器,直到那时,我会重新加载该表?

编辑: 我修改了我的代码并将该函数带入了我的MainMenuViewController,现在它构建但从不显示数据。

- (void) loadMatchData
{
    NSMutableArray* __block blockGames = currentGames;
    MainMenuViewController* __weakSelf = self;

    [GKTurnBasedMatch loadMatchesWithCompletionHandler:^(NSArray *matches, NSError *error)
     {
         if (matches)
         {
             for (int i = 0; i < matches.count; i++)
             {
                 [(GKTurnBasedMatch*)matches[i] loadMatchDataWithCompletionHandler: ^(NSData *matchData, NSError *error)
                  {
                      int size = [matchData length];
                      if (size != 0)
                      {
                          Game* game = [NSKeyedUnarchiver unarchiveObjectWithData:matchData];
                          [blockGames addObject:game];
                      }
                      else
                      {
                          Game* game = [[Game alloc] init];
                          [blockGames addObject:game];
                          game.activePlayer = [GKLocalPlayer localPlayer];
                      }
                      [__weakSelf.tableView reloadData];
                  }];
             }
         }
         [__weakSelf.tableView reloadData];
     }];
    [__weakSelf.tableView reloadData];
}

现在在我的ViewDidLoad中,我只是打电话:

[self loadMatchData];

1 个答案:

答案 0 :(得分:0)

亲爱的,亲爱的。不要用“while”循环停止程序执行! 为什么不在块的末尾简单地调用[self.tableView reloadData]

所以,

  1. 删除viewDidLoad方法
  2. 中的最后两行
  3. *blockReturn = true;替换为[self.tableView reloadData](您可能需要保留对'self'的弱引用以避免保留周期)
  4. 永远不要使用while (this and that)等待操作完成。无响应的用户界面很糟糕,会导致用户放弃您的应用。