LoginSignup
0
1

More than 3 years have passed since last update.

【Xcode】UIAlertView → UIAlertController

Last updated at Posted at 2020-03-28

先日書いた
【XCode:警告ログ】UIAlertView is deprecated.
の記事には書きましたが UIAlertView → UIAlertController に切り替えに困る人がいるんじゃないかなというところでいくつか考えてみました

iOS__UIAlertView.h

iOS__UIAlertView.h
#import <UIKit/UIKit.h>

@class iOS8__UIAlertView;
/// UIAlertViewDelegate の以降用
@protocol iOS8__UIAlertViewDelegate

- (void)iOS8__alertView:(iOS8__UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

@end

@interface iOS8__UIAlertView : UIViewController <iOS8__UIAlertViewDelegate>

@property (retain,nonatomic) NSString *message;

/// UIAlertViewDelegate の以降用
@property (assign,nonatomic) id<iOS8__UIAlertViewDelegate>delegate;
@property (assign,nonatomic) NSInteger cancelButtonIndex;

+ (void)iOS__alert;

iOS__UIAlertView.m

iOS__UIAlertView.m
#import "iOS8__UIAlertView.h"

/// ここ、(AppDelegate)を利用する理由はのちにわかります。
#import "AppDelegate.h"

@interface iOS8__UIAlertView ()

/// ボタンの一覧をプライベートのパラメーターを用意
@property (retain,nonatomic) NSArray *alertButtons;

@end

@implementation iOS8__UIAlertView

/// サンプル用のアラート
+ (void)iOS__alert {
    /// 変更箇所
    /// UIAlertView *alert = [[iOS8__UIAlertView alloc]
    iOS8__UIAlertView *alert = [[iOS8__UIAlertView alloc] initWithTitle:@"タイトル"
                                                    message:@"メッセージ"
                                                   delegate:self
                                          cancelButtonTitle:@"キャンセル"
/// 変更箇所
///                                          otherButtonTitles:@"ボタン1", @"ボタン2", @"ボタン3",nii];
                                          otherButtonTitles:@[@"ボタン1", @"ボタン2", @"ボタン3"]];

    [alert show];
}

- (instancetype)initWithTitle:(NSString *)title
                      message:(NSString *)message
                     delegate:(id)delegate
            cancelButtonTitle:(NSString *)cancelButtonTitle
            otherButtonTitles:(NSArray *)otherButtonTitles {
    self = [super init];
    if (self) {
        self.title = title;
        self.message = title;
        self.delegate = delegate;
        // self.cancelButtonTitle = cancelButtonTitle;
        // self.otherButtonTitles = otherButtonTitles;
        NSMutableArray *mAlertButtons = [NSMutableArray new];
        if (otherButtonTitles) {
            mAlertButtons = [otherButtonTitles mutableCopy];
        }
        if (cancelButtonTitle) {
            [mAlertButtons addObject:cancelButtonTitle];
        }
        self.alertButtons = [mAlertButtons copy];
    }
    return self;
}

- (void)show {
  /// ここでAppDelegate の準備します。
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:self.title message:self.message preferredStyle:UIAlertControllerStyleAlert];

    /// ボタンの数だけボタンアクションを用意します。
    for (int i = 0; i < self.alertButtons.count;i++) {
        NSString *title = self.alertButtons[ i ];
        [alertController addAction:[UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self iOS8__alertView:self clickedButtonAtIndex:i];
        }]];
    }
  /// ここでAppDelegate から rootViewController に表示させます。
    [appDelegate.window.rootViewController presentViewController:alertController animated:YES completion:^{

    }];
}

/// 変更箇所
/// - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// デリゲートで押したボタンを判別
- (void)iOS8__alertView:(iOS8__UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    switch (buttonIndex) {
        case 0: {
            // 0番目のボタン
            break;
        }
        case 1: {
            // 1番目のボタン
            break;
        }
        case 2: {
            // 2番目のボタン
            break;
        }
        case 3: {
            // 3番目のボタン
            break;
        }
            // …以下必要な分続く
    }
}

@end 

こんな感じですよね。

また色々と変更していくと思いますので、iOS8 対応できるように一つ一つ片付けていきましょう!

現場では、今まで動いていたというところがバージョンアップに従って変更されてしまうことで対応しなければなりません。これのおかげで、IT現場が食い扶持がなくなることはないと言われております。
ただこれを対応するのは結構大変だったりします。いつも新しい情報を取り入れていないといけなかったり、サービスが終わってしまったなんてこともありますからね。

契約する時にこのバージョンアップの対応に関してのことを示しておくといいかもですね。

関連記事

【About】(http://qiita.com/sunstripe) - サンストライプ


制作チーム:サンストライプ

sunstripe_logo.png
http://sunstripe.main.jp/

(月1WEBコンテンツをリリースして便利な世の中を作っていくぞ!!ボランティアプログラマー/デザイナー/イラストレーター/その他クリエイター声優募集中!!)

地域情報 THEメディア

THE メディア 地域活性化をテーマに様々なリリース情報も含め、記事をお届けしてます!!
https://the.themedia.jp/

ゼロからはじめる演劇ワークショップ

多様化の時代に向けて他者理解を鍛える

プログラミングワークショップ・ウェブ塾の開講!!!

様々なテーマでプログラミングに囚われずに取り組んでいきます。
詳しくはこちら ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
プログラミングサロン 月1だけのプログラミング学習塾

協力応援 / 支援者の集い

チーム:サンストライプ

プログラミングラボ

一緒にポートフォリオを作りませんか?現場の体験やそれぞれの立場から年齢関係なく作品を作りたい方々と一緒にチームを作って、作品を作っています。現場に行きたい人には、職場紹介や職場の体験や悩み相談なども受けております。
様々な職種からプログラミングの知識を得たい、デザインの知識を得たい、データーベースの知識を得たいという人が集まっております。
週1のミーティングにそれぞれの近況と作業報告して、たまにリモート飲み会などをしております!!

興味がある方は、DMに話しかけてみてください。

トラストヒューマン

http://trusthuman.co.jp/
私たちは何よりも信頼、人と考えてます。

「コンサルティング」と「クリエイティブ」の両角度から「人材戦略パートナー」としてトータル的にサポートします!!

キャリア教育事業
広域学習支援プラットフォーム『のびのび日和』
https://slc-lab.amebaownd.com/

スポンサー募集

ネリム

https://nerim.co.jp/
配信事業などを映像コンテンツなどの制作しております

ビヨンドXプロジェクト

ビヨンドXプロジェクト全体会議

0
1
0

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
0
1