2013-04-30 64 views
0

我想拉动玩家参与的回合制游戏,以便填充我的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]; 
+0

刚开始您错过的iPhoneBackground-568h'文件扩展名为 – Popeye 2013-04-30 09:19:53

+0

这没什么了,它是一个PNG格式。 – 2013-04-30 16:56:54

+0

你确定吗?我可能会测试这个理论。不是说如果不是必需的,它会有所作为。很高兴知道以供将来参考。 – Popeye 2013-04-30 18:53:04

回答

0

哦,亲爱的。不要用“while”循环停止程序执行! 为什么不简单地在街区结束时拨打[self.tableView reloadData]

所以,

  1. viewDidLoad方法删除最后两行
  2. 更换*blockReturn = true;[self.tableView reloadData](您可能需要保持一个弱引用“自我”,以避免保留周期)
  3. 决不曾经使用while (this and that)等待操作完成。不响应的用户界面不好,它会导致用户放弃你的应用程序。
+0

嗯。我试着做你说的话,它会建立并不会崩溃,但不会显示数据。我正在编辑新版本的原始帖子。 – 2013-04-30 16:55:50

+0

在'for'循环中删除这一行'[__weakSelf.tableView reloadData];'。在此行的第二个出现处放置一个断点并确保'blockGames'包含数据。 – 2013-04-30 23:11:45