Aqlier/ 7月 14, 2018/ iPhone

Xcode 開発で、iOS 9.0以降でアラートメッセージを表示させる方法をまとめました。

ーーーエラーメッセージーー
‘UIAlertView’ is deprecated: first deprecated in iOS 9.0 – UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead

ーーー日本語訳ーーー
‘UIAlertView’は推奨されていません.iOS 9.0では最初に非推奨です。 – UIAlertViewは推奨されていません。 代わりにUIAlertControllerStyleAlertのpreferredStyleを使用してUIAlertControllerを使用してください

//修正前
 UIAlertView *alert = [[UIAlertView alloc]
                                   initWithTitle:@"アラートメッセージ"
                                   message:@"メッセージ詳細"
                                   delegate:self
                                   cancelButtonTitle:@"Cancel"
                                   otherButtonTitles:@"OK", nil];

  [alert show];


//修正後
         UIAlertController *alertController = [UIAlertController 
		alertControllerWithTitle:@"アラートメッセージ" 
		message:@"メッセージ詳細" 
		preferredStyle:UIAlertControllerStyleAlert];
    
     [self presentViewController:alertController animated:YES completion:nil];
    
        [alertController addAction:[UIAlertAction actionWithTitle:@"OK" 
style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            // ボタンアクション
//            [self OK_sec];
        }]];
    
//
        [alertController addAction:[UIAlertAction actionWithTitle:@"NG" 
style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            // ボタンアクション
//            [self NG_sec];
        }]];
   
    [alertController addAction:[UIAlertAction actionWithTitle:@"cancel" 
style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        // ボタンアクション
        //            [self Cancel_sec];
    }]];
}


// ボタンアクション
- (void)OK_sec {

}

// ボタンアクション
- (void)NG_sec {

}

// ボタンアクション
- (void)Cancel_sec {

}