cd ~/appstars
~ / objective-c / ios8%e3%81%8b%e3%82%89uialertview%e3%81%8c%e5%bb%83%e6%ad%a2-%e4%bb%a3%e3%82%8f%e3%82%8a%e3%81%ab%e4%bd%bf%e3%81%86%e3%81%ae%e3%81%afuialertcontroller

iOS8からUIAlertViewが廃止 代わりに使うのはUIAlertController

2014 . 11 . 18 #objective-c by Hoshino

iOS8からUIAlertViewのコールバックが上手く動かないなー
あとそういえば4日もヨーグルト以外何も食べてないなー外出るのめんどいなー
といいつつ、サイフ片手に焼き肉弁当と幕の内弁当とおにぎり3つを買いにコンビニへ行こうとしているそこのあなた。

食べ過ぎです。

そこで今回はiOS8から仲間入りしたUIAlertControllerをどのように使うのか、
iOS8とiOS7以下を場合分けで対応したコードを書いてみましょう。(BlocksKit使用)

えいやっ

NSString* message = @”この画像を削除しますか”; //実際は「message」を「massage」にするとちょっと面白い

//iOS8以上
if([UIAlertController class]) {
        UIAlertController* ac = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
        [ac addAction:[UIAlertAction actionWithTitle:@”削除” style:UIAlertActionStyleDefault handler:^(UIAlertAction* action) {
                //なんか実行
        }]];
        [ac addAction:[UIAlertAction actionWithTitle:@”キャンセル” style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:ac animated:YES completion:nil];
}
//iOS7以下
else {
        UIAlertView* av = [[UIAlertView alloc] bk_initWithTitle:nil message:message];
        [av bk_addButtonWithTitle:@”削除” handler:^{
                //なんか実行
        }];
        [av bk_addButtonWithTitle:@”キャンセル” handler:nil];
        [av show];
}

なんとなく形は似てますね。

UIAlertControllerの方はUIViewControllerなので、
現コン(現在のUIViewController)からpresentViewControllerしなければなりませぬので注意。

あと季節の変わり目は体調管理に注意。