代替として新たに登場した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
【UIAlertControllerの最新記事】