2

A と B の2 つがUITableViewControllersあり、A のテーブル ビュー セルをクリックすると、次のようになります。

  1. B の変数のいくつかを A から設定して、A から B へのセグエを準備します。
  2. A から B へのセグエを実行します。
  3. Bが登場。
  4. で「読み込み中」アクティビティ インジケータを表示し[MBProgressHUD][1]ます。
  5. バックグラウンド タスクで、URL からデータを取得します。
  6. URL リクエストでエラーが発生した場合 (データが受信されないか、ステータス コードが 200 以外の場合)、(a) アクティビティ インジケーターを非表示にし、(b) エラー メッセージを含む UIAlertView を表示します。
  7. それ以外の場合、(a) 取得したデータで B をリロードtableViewし、次に (b) アクティビティ インジケータを非表示にします

ただし、これが起こっていることであり、修正方法がわかりません。

  1. A のセルをクリックすると、B が右側から空のプレーンにスライドインしますUITableView。表示されMBProgressHUD ません
  2. しばらくするtableViewと、取得したデータがリロードされ、 がMBProgressHUD非常に短時間表示されます。
  3. MBProgressHUDすぐに消えます。

バックグラウンド タスクの実行方法にエラーはないようです。MBProgressHUD私の問題は、 B ビュー コントローラーが表示されたらすぐにアクティビティ インジケーターを表示するにはどうすればよいですか? (実際、なぜ表示されないのでしょうか?) コードは以下のとおりです。

Aの prepareForSegue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    B *b = (B *)[segue destinationViewController];

    // Set some of B's variables here...
}

関連するメソッドB

- (void)viewDidAppear:(BOOL)animated {
    [self startOver];
}

- (void)startOver {
    [self displayLoadingAndDisableTableViewInteractions];
    [self retrieveListings];
    [self.tableView reloadData];
    [self hideLoadingAndEnableTableViewInteractions];
}

- (void)displayLoadingAndDisableTableViewInteractions {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"Loading";
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    self.tableView.userInteractionEnabled = NO;
}

- (void)hideLoadingAndEnableTableViewInteractions {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    self.tableView.userInteractionEnabled = YES;
}

- (void)retrieveListings {
    __block NSArray *newSearchResults;

    // Perform synchronous URL request in another thread.
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        newSearchResults = [self fetchNewSearchResults];
    });

    // If nil was returned, there must have been some error--display a UIAlertView.
    if (newSearchResults == nil) {
        [[[UIAlertView alloc] initWithTitle:@"Oops!" message:@"An unknown error occurred. Try again later?" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    } else {
        // Add the retrieved data to this UITableView's model. Then,
        [self.tableView reloadData];
    }
}

- (NSArray *)fetchNewSearchResults {
    // Assemble NSMutableArray called newSearchResults from NSURLConnection data.
    // Return nil if an error or a non-200 response code occurred.
    return newSearchResults;
}
4

3 に答える 3

0

MBProgressHUD の一般的な方法を使用しました。

  1. #import "MBProgressHUD.h" in AppDelegate.hも以下のメソッドに従ってください。

    - (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title;
    - (void)dismissGlobalHUD;
    
  2. AppDelegate.m に次のメソッドを追加します。

    - (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title {
       [MBProgressHUD hideAllHUDsForView:self.window animated:YES];
       MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.window animated:YES];
       hud.labelText = title;
       return hud;
    }
    
    - (void)dismissGlobalHUD {
      [MBProgressHUD hideHUDForView:self.window animated:YES];
    }
    
  3. 使い方?

    AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    //Show Indicator
    [appDel showGlobalProgressHUDWithTitle:@"Loading..."];
    //Hide Indicator
    [appDel dismissGlobalHUD];
    

お役に立てれば。

于 2013-07-24T10:15:37.767 に答える