2013-04-30 2 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

+0

그것은 PNG입니다. –

+0

정말입니까? 그 이론을 시험해 볼 수도 있습니다. 그것이 필요하지 않다면 그것은 차이를 만든다. 나중에 참조 할 때 알아두면 좋습니다. – Popeye

답변

0

오, 친애하는. "while"루프로 프로그램 실행을 중단하지 마십시오! 블록 끝에서 [self.tableView reloadData]으로 전화하지 않으시겠습니까?

그래서,

  1. viewDidLoad 방법의 마지막 두 줄을 제거
  2. [self.tableView reloadData]*blockReturn = true;
  3. 안 (당신이 사이클을 유지 피하기 위해 '자기'에 약한 참조를 유지해야 할 수도 있습니다)를 교체 계속 while (this and that)을 사용하여 작업이 완료 될 때까지 대기하십시오. 응답하지 않는 UI가 좋지 않아 사용자가 앱을 포기하게됩니다.
+0

흠. 나는 당신이 말한 것을 시도해 보았고, 빌드하고 충돌하지는 않지만 결코 데이터를 표시하지 않습니다. 새 버전으로 원래 게시물을 편집하고 있습니다. –

+0

'for' 루프에서'[__weakSelf.tableView reloadData];를 삭제하십시오. 이 줄의 두 번째 발생에 중단 점을 넣고'blockGames'가 데이터를 포함하고 있는지 확인하십시오. –

관련 문제