59
59

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOS8でUIAlertViewはこうやってUIAlertControllerに置き換える

Last updated at Posted at 2014-08-11

iOS8対応をするに至ってUIAlertViewUIAlertControllerに置き換えなければいけないのでメモ。

Objective-Cだと、こう。

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"タイトル" message:@"メッセージ" preferredStyle:UIAlertControllerStyleAlert];

[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    // ボタンが押された時の処理
}]];

[self presentViewController:alert animated:YES completion:^{
    // 表示完了時の処理
}];

あとはOSのバージョンとかで分岐すればOK。

ちなみにSwiftだと、こう。

var alert:UIAlertController = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {
    // ボタンが押された時の処理
}))

self.presentViewController(alert, animated: true, completion: {
    // 表示完了時の処理
})

Objective-Cよりもシンプル。

ボタンの追加も分かりやすいし、何よりデリゲートで処理しなくてよくなったのはめちゃくちゃ良いと思う。

59
59
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
59
59

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?