LoginSignup
110
111

More than 5 years have passed since last update.

iOS8でのアクションシート:廃止されるUIActionSheetと推奨されるUIAlertController

Last updated at Posted at 2014-07-12

iOSでアクションシート表示用のクラスUIActionSheetがiOS8からdeprecatedになるため、
UIActionSheetを使用した際の挙動と推奨されているUIAlertControllerの使い方についてまとめました。

(本記事の内容は一般公開されているiOS8の情報に基づいています。
iOS8に関するスクリーンショットは掲示していません。)

iOS8で廃止されるUIActionSheetを使用した際の挙動

deprecatedされたメソッドを使用した場合、アプリが落ちるなどの問題が発生することがありますが、
iOS8 beta3の環境ではシミュレータ・実機ともに問題なく表示できます。
2014/7/12現在では継続してUIActionSheetを使用したままでも問題無いようです。

公式にdeprecatedと宣言されているため挙動が変わる可能性は否定できません。
推奨されているUIAlertControllerへの移行をおすすめします。

アクションシートの使い所

iOS7_ActionSheet.jpg

ユーザへの注意喚起の方法はアクションシート以外にもダイアログの表示があります。
どのように使い分けるかは「iOS ヒューマンインターフェース ガイドライン」でAppleが公式に以下のように述べています。

条件 推奨UI
ユーザが承認したタスクの確認を求める場合 アクションシート
致命的なエラーをユーザに通知する場合 ダイアログ

推奨されているUIAlertControllerのメリット

これまでのUIActionSheetにはtitleしかありませんでしたが、
新たなUIAlertControllerにはmessageのパラメータが追加されています。
タイトルの下に説明文を表示できるようになりました。

iOS7以前のOSをサポートする際の注意点

開発中のアプリの中にはiOS7以前をサポートするアプリもあるかと思います。
そのためにはUIActionSheetとUIAlertControllerをコード内で併用して使い分けることが必要です。

その際にボタンが押された時の処理に注意してください。
UIActionSheetではdelegateメソッドで処理しますが、
UIAlertControllerはクロージャで処理します。

UIActionSheetとUIAlertControllerを併用する際にはボタンが押された時のメソッドを作成して、
そのメソッドを両者で使い回すことをオススメします。

サンプルコード

UIAlertController

Objective-C

UIAlertController

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"UIAlertControllerStyle.ActionSheet" message:@"iOS8" preferredStyle:UIAlertControllerStyleActionSheet];

[alertController addAction:[UIAlertAction actionWithTitle:@"はい" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    // otherボタンが押された時の処理                                                      
    [self otherButtonPushed];
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"いいえ" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    // cancelボタンが押された時の処理                                                      
    [self cancelButtonPushed];
}]];

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

- (void)cancelButtonPushed {}
- (void)otherButtonPushed {}

UIActionSheet (deprecated in iOS8)

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"ActionSheet"
                                                             delegate:nil
                                                    cancelButtonTitle:@"いいえ"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"はい",nil];
[actionSheet showInView:self.view];

...

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (buttonIndex) {
        case 0:
            // otherボタンが押されたときの処理
            [self otherButtonPushed];
            break;
    }
}

- (void)actionSheetCancel:(UIActionSheet *)actionSheet {
    // cancelボタンが押された時の処理
    [self cancelButtonPushed];
}

- (void)cancelButtonPushed {} 
- (void)otherButtonPushed {} 

Swift

UIAlertController

var alertController = UIAlertController(title: "UIAlertControllerStyle.ActionSheet", message: "iOS8", preferredStyle: .ActionSheet)

let otherAction = UIAlertAction(title: "はい", style: .Default) {
    action in NSLog("はいボタンが押されました")
}
let cancelAction = UIAlertAction(title: "いいえ", style: .Cancel) {
    action in NSLog("いいえボタンが押されました")
}

alertController.addAction(otherAction)        
alertController.addAction(cancelAction)

presentViewController(alertController, animated: true, completion: nil)

iPadでのcancelボタンの非表示

iPadではアクションシートをポップオーバー外に表示させた場合、cancelボタンが表示されません。
これは「アクションシートの外側をタップすればアクションシートを消せるためcancelボタンは不要である」という公式のポリシーによるものです。

参考

公式Class Reference
https://developer.apple.com/library/prerelease/iOS/documentation/UIKit/Reference/UIAlertController_class/index.html#//apple_ref/occ/cl/UIAlertController

110
111
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
110
111