UIAlertControllerとは?
UIAlertControllerとは下記画像のようなダイアログを表示するためのクラスです。
今回はこのクラスを使い、Objective-Cでダイアログの実装を行う方法を紹介していきます。
サンプルコード
//下記のように記述する事で、UIAlertControllerを初期化します。
//alertControllerWithTitleに指定した文字列が太文字で表示されます。
//messageに指定した文字列が小見出し的な感じで、小文字で表示されます。
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"UIAlertControllerStyle.Alert" message:@"iOS8" preferredStyle:UIAlertControllerStyleAlert];
//下記のコードでボタンを追加します。また{}内に記述された処理がボタン押下時の処理なります。
[alertController addAction:[UIAlertAction actionWithTitle:@"はい" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//ボタンがタップされた際の処理
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"いいえ" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//ボタンがタップされた際の処理
}]];
//下記のコードでダイアログを表示します。
[self presentViewController:alertController animated:YES completion:nil];