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];
4

1 回答 1

0

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

所以,

  1. viewDidLoad删除方法中的最后两行
  2. 替换*blockReturn = true;[self.tableView reloadData](您可能需要保留对“自我”的弱引用以避免保留循环)
  3. 永远不要使用while (this and that)等待操作完成。无响应的 UI 很糟糕,它会导致用户放弃您的应用。
于 2013-04-30T09:05:31.767 回答