Copyright (C) iPhoneアプリ開発備忘録 All rights reserved.
ブログ内で記したコード、内容の正確性は保証いたしません。
記載内容を実装したことにより発生した不具合・損害等の責任は一切負いません。



2014年09月29日

iOS8でUIAlertControllerを使用する



UIActionSheetはiOS8でDeprecatedとなった。
代替として新たに登場したUIAlertControllerを使用しなければならない。


iOS8.0.2では、DeprecatedとなったUIActionSheet はiPhoneで動作するが、iPadでは動作しなかったり、動作しても- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndexメソッドが複数回呼ばれたりと、おかしな動作が起こることが確認されたので、iOS8で動作させる場合はUIAlertControllerを使用するように修正する必要がある。

hogeViewController.h
@interface hogeViewController : UIViewController <UIPopoverControllerDelegate>


hogeViewController.m
if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) { // iOS8.0以降

// コントローラを生成
UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"Info"
message:@"Please Select"
preferredStyle:UIAlertControllerStyleActionSheet];

// OK用のアクションを生成
UIAlertAction *okAction =
[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// ボタンタップ時の処理

}];

// Destructive用のアクションを生成
UIAlertAction *destructiveAction =
[UIAlertAction actionWithTitle:@"Destructive"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
// ボタンタップ時の処理

}];

// Cancel用のアクションを生成
UIAlertAction *cancelAction =
[UIAlertAction actionWithTitle:@"Cancel"
style: UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
// ボタンタップ時の処理

}];

// コントローラにアクションを追加
[ac addAction:okAction];
[ac addAction:destructiveAction];
[ac addAction:cancelAction];

// アクションシート表示処理
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

CGRect frame = [[UIScreen mainScreen] applicationFrame];

ac.popoverPresentationController.sourceView = self.view;
ac.popoverPresentationController.sourceRect = CGRectMake(CGRectGetMidX(frame)-60,frame.size.height-50, 120,50);

}

[self presentViewController:ac animated:YES completion:nil];

} else { // iOS8よりも前
// UIActionSheetの処理

}


ボタンタップ時の処理は、UIAlertActionのそれぞれのブロックの中に記述する。

iPadの場合、popoverPresentationControllerを使用してポップオーバーで表示しなくてはならない。
ちなみに、UIPopoverControllerはiOS8でDeprecatedとなっている。

iPadの画面中心に表示させる場合は、下記のようなコードにする。
ac.popoverPresentationController.sourceView = self.view;
ac.popoverPresentationController.sourceRect = self.view.bounds;
ac.popoverPresentationController.permittedArrowDirections = 0; // 吹き出しの矢印を表示しない


iPadのNavigationBar上のボタンから吹き出すように表示させる場合は、下記のようなコードにする。
- (void)buttonAction:(id)sender
{
ac.popoverPresentationController.sourceView = self.view;
ac.popoverPresentationController.barButtonItem = sender;
}

または、
ac.popoverPresentationController.sourceView = self.view;
ac.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;


選択したUITableViewCellから吹き出したように表示する場合は、下記のようなコードにする。
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
CGRect frame = [tableView rectForRowAtIndexPath:indexPath];

ac.popoverPresentationController.sourceView = self.view;
ac.popoverPresentationController.sourceRect = CGRectMake(frame.origin.x, (frame.origin.y - 15), frame.size.width, frame.size.height);
}


ActionSheetではなく、AlertViewとして表示する場合は、下記のコードにする。
preferredStyle:UIAlertControllerStyleAlert

UIAlertControllerStyleAlertを指定すると、自動的に画面中心に表示される。iPadでのpopoverPresentationController.sourceViewの指定は不要。

参考記事:
https://developer.apple.com/LIBRARY/PRERELEASE/IOS/documentation/UIKit/Reference/UIAlertAction_Class/index.html
https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIAlertController_class/index.html
http://stackoverflow.com/questions/25882133/change-action-sheet-popover-arrow-in-ios8
http://qiita.com/ShingoFukuyama/items/c3c83c552292dcb49457
タグ:IOS Mac apple iPAD iPhone
posted by mobileDeveloper at 14:22 | Comment(0) | TrackBack(0) | UIAlertController はてなブックマーク - iOS8でUIAlertControllerを使用する | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
※ブログオーナーが承認したコメントのみ表示されます。

この記事へのトラックバック
Apple、Appleのロゴ、App Store、iPodのロゴ、iTunesは、米国および他国のApple Inc.の登録商標です。
iPhone、iPod touch、iPadはApple Inc.の商標です。
iPhone商標は、アイホン株式会社のライセンスに基づき使用されています。
その他、本ブログに記載されている製品名、会社名は、それぞれ各社の商標または登録商標です。