[iPhone]ActionSheetでCancelボタンが効かない問題


現在作成中のiPhoneアプリでダウンロード中の状態表示をするようにしてみた。

ActionSheetを作成して、それにUIProgressViewを貼り付けるまでは簡単だった。

オブジェクトをコントローラクラスに追加し、

	UIActionSheet	*actionSheet;
	UIProgressView *progressBar;
	UILabel			*progressLabel;

ViewDidLoadでActionSheetを作成

-(void)createActionSheet {

	if(!actionSheet) {
		actionSheet = [[UIActionSheet alloc] initWithTitle:@"Please wait...\n\n\n\n"
												  delegate:self
										 cancelButtonTitle:NSLocalizedString(@"Cancel",nil)
									destructiveButtonTitle:nil
										 otherButtonTitles:nil];

		actionSheet.actionSheetStyle = UIBarStyleBlackTranslucent;

	}
	if(!progressBar) {

		progressBar = [[UIProgressView alloc] initWithFrame:CGRectMake(30.0f, 40.0f, 260.0f, 90.0f)];
		progressBar.progressViewStyle = UIProgressViewStyleDefault;
		progressBar.progress = 0.0f;
		[actionSheet addSubview:progressBar];

		progressLabel = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 50.0f, 240.0f, 20.0f)];
		progressLabel.backgroundColor = [UIColor clearColor];
		progressLabel.textColor = [UIColor whiteColor];
		progressLabel.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
		progressLabel.text = NSLocalizedString(@"Downloading...", nil);

		[actionSheet addSubview:progressLabel];
	}

}

ダウンロードの開始のタイミングでActionSheetを作成

- (IBAction)startDownload:(NSString*)genreId {
	downloadedContentLength = 0;
	progressBar.progress = 0;
	progressLabel.text = NSLocalizedString(@"Downloading", nil);
	[actionSheet showInView:self.view];
//  以下略

ダウンロードの処理の途中にUIProgressViewを更新してやると完成
img1

ここまではサンプルコード通りにやれば簡単だった。

しかしどうやってもキャンセルボタンが効かない。

ViewDidLoadでActionSheetを表示させるとキャンセルボタンが効くが画面の真ん中に表示されてしまう。

ActionSheetのボタンがキャンセルボタン一つだとうまくいかないが、ボタンを増やすとキャンセルが効く・・・。???
showInViewを使わずに、showFromTabBarを使うと上からアクションシートが降りてくるがキャンセルボタンが効く。

ググると同じような問題がたくさんあって、うまく動かないので別のスレッドを使えととかかいてある。

初期化のタイミングを変えたりスレッド化したり一晩さんざん悩んだあげく、よくよく調べてみるとSDK2.2のバグらしい。

んで、いろいろ調べた結果一番エレガントな回避コードは

こいつを

[actionSheet showInView:self.view];

こうしてやることだった。

[actionSheet showInView:self.view.window];

img2

tAkatronixおすすめのiPhone開発本

iPhone デベロッパーズ クックブック
Erica Sadun
ソフトバンククリエイティブ
売り上げランキング: 84906
詳解 Objective-C 2.0
詳解 Objective-C 2.0
posted with amazlet at 10.01.31
荻原 剛志
ソフトバンククリエイティブ
売り上げランキング: 4163
iPhoneプログラミングUIKit詳解リファレンス
所 友太
リックテレコム
売り上げランキング: 3089
iPhone Core Audioプログラミング
永野 哲久
ソフトバンククリエイティブ
売り上げランキング: 22615
Bookmark and Share

関連記事

    1. No comments yet.
    (will not be published)