ios9, iPod touch, xcode7 初心者です。
alert view を表示させようと参考書のプログラムを
ダウンロードしたところ、 UIAlertView は iOS9 では、
deprecated になったとのこと。 部分的な記述はいくつかあったのですが、全体の中でどのように使えばいいのか、イメージがつかめていません。そこで、UIAlertController のサンプルコード(プロジェクトを含む)をさがしています。 どなたか、参考になるようなHPなど、ご教示頂けましたら幸いです。
宜しくお願い致します。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
Objective-C
参考にするサイトとして、以下はいかがでしょうか?
iOS8でのダイアログ表示:廃止になるUIAlertViewと推奨されているUIAlertControllerのメリット
★ リンクを参考にしたサンプル
objc
1 2// ボタン押下時にAlertを表示する 3- (IBAction)pushButton:(UIButton *)sender { 4 5 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"タイトル" message:@"メッセージ" preferredStyle:UIAlertControllerStyleAlert]; 6 7 [alertController addAction:[UIAlertAction actionWithTitle:@"はい" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 8 // 「はい」ボタンが押された時の処理 9 [self otherButtonPushed]; 10 }]]; 11 [alertController addAction:[UIAlertAction actionWithTitle:@"いいえ" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 12 // 「いいえ」ボタンが押された時の処理 13 [self cancelButtonPushed]; 14 }]]; 15 16 // 画面に表示します 17 [self presentViewController:alertController animated:YES completion:nil]; 18} 19 20// アラートのはいボタン押下処理 21- (void)otherButtonPushed { 22 NSLog(@"はい!"); 23} 24 25// アラートのいいえボタン押下処理 26- (void)cancelButtonPushed { 27 NSLog(@"いいえ!"); 28}
Swift
Swift:UIAlertController でアラートを表示するサンプルコード
※Swift2ではないですがprintln だけ printに置き換えると大丈夫だと思います
★ 簡単なサンプル
swift
1 2// ボタン押下時にアラートを表示します 3@IBAction func pushButton(sender: AnyObject) { 4 5 let alert = UIAlertController(title: "title", message: "message", preferredStyle: .Alert) 6 7 let action1 = UIAlertAction(title: "Action1", style: .Default, handler: { action in 8 print("Action1") 9 }) 10 11 let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: { action in 12 print("Cancel") 13 }) 14 15 alert.addAction(action1) 16 alert.addAction(cancel) 17 18 presentViewController(alert, animated: true, completion: nil) 19}
★ もっともシンプルなアラート
swift
1// ユーザーにお知らせするだけのアラート 2@IBAction func pushButton(sender: AnyObject) { 3 4 let alert = UIAlertController(title: "title", message: "message", preferredStyle: .Alert) 5 alert.addAction( UIAlertAction(title: "OK", style: .Default, handler: nil)) 6 presentViewController(alert, animated: true, completion: nil) 7}
投稿2016/05/01 07:07
編集2016/05/01 07:27総合スコア8490
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/01 07:11
2016/05/01 07:20
2016/05/10 05:14
2016/06/18 02:03