A と B の2 つがUITableViewControllers
あり、A のテーブル ビュー セルをクリックすると、次のようになります。
- B の変数のいくつかを A から設定して、A から B へのセグエを準備します。
- A から B へのセグエを実行します。
- Bが登場。
- で「読み込み中」アクティビティ インジケータを表示し
[MBProgressHUD][1]
ます。 - バックグラウンド タスクで、URL からデータを取得します。
- URL リクエストでエラーが発生した場合 (データが受信されないか、ステータス コードが 200 以外の場合)、(a) アクティビティ インジケーターを非表示にし、(b) エラー メッセージを含む UIAlertView を表示します。
- それ以外の場合、(a) 取得したデータで B をリロード
tableView
し、次に (b) アクティビティ インジケータを非表示にします
ただし、これが起こっていることであり、修正方法がわかりません。
- A のセルをクリックすると、B が右側から空のプレーンにスライドインします
UITableView
。表示されMBProgressHUD
ません。 - しばらくする
tableView
と、取得したデータがリロードされ、 がMBProgressHUD
非常に短時間表示されます。 - は
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;
}