今作っている位置情報通知アプリで、送信にそれなりに時間がかかるので、送信中には単なるUIActivityIndicatorViewによるぐるぐる表示だけではなく、モーダルダイアログ的なものを表示したいと考えた。
自分で作るのはおっくうだと思っていたら、MBProgressHUD なるものがMOONGIFTで紹介されていたので、利用してみた。
組み込みは MBProgressHUD.h と MBProgressHUD.m をプロジェクトに追加するだけなのでとても簡単だ。
今回は NSURLConnection でデータを送信しており、送信終了は – (void)connectionDidFinishLoading:(NSURLConnection *)connection で受け取っている。
このため、MBProgressHUD のサンプルプログラムそのままでは使えないのだが、StackOverflow で使い方が紹介されていたので参考にしたらさくっと導入できた。
これは便利。
mbProcess=[[MBProgressHUD alloc] initWithView:self.view]; mbProcess.labelText=@"Loading Data"; [self.view addSubview:mbProcess]; [mbProcess setDelegate:self]; [mbProcess show:YES]; ... // 処理が終わったところで [mbProcess hide:YES];
delegateの実装も忘れずに。
// delegate の処理を書く必要がある
#pragma mark -
#pragma mark MBProgressHUDDelegate methods
- (void)hudWasHidden {
// Remove HUD from screen when the HUD was hidded
[mbProcess removeFromSuperview];
[mbProcess release];
}
MBProgressHUDのサンプルプログラムの動作の様子はこちら。
追記 : 2010/10/11
UITableView を表示する際に MBProgressHUD を表示したいことはよくありそうだ。
Loading screen in TableView – iPhone Dev SDK Forumが参考になる。
自分もはまったが、-(void)viewDidAppear:(BOOL)animated に実装すればうまくいく。
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// The hud will dispable all input on the view
HUD = [[MBProgressHUD alloc] initWithView:self.view];
// Add HUD to screen
[self.view addSubview:HUD];
// Regisete for HUD callbacks so we can remove it from the window at the right time
HUD.delegate = self;
HUD.labelText = @"Loading";
// Show the HUD while the provided method executes in a new thread
[HUD showWhileExecuting:@selector(doTimeConsumingJob) onTarget:self withObject:nil animated:YES];
}
追記: 2012/01/14
かなり久しぶりにMBProgressHUDを使おうと思ったらすっかり忘れていて、もっと簡単に使えるものがないか調べてみたところ SVProgressHUD が見つかった。
単純に処理中表示を出したいのであればこちらの方が簡単そう。