UIalertView is deprecated問題

2625 ワード

Xcode 7でiOS 9.0のSDKでは、UIAlertViewの使用は推奨されず、UIAlertControllerのみが使用できることが明らかになった.
前のポップアップ・プロンプト・ボックスのサンプル・コードは次のとおりです.
#import "ViewController.h" 
 
@interface ViewController () 
 
@property(strong,nonatomic) UIButton *button; 
 
@end 
 
@implementation ViewController 
 
- (void)viewDidLoad { 
[super viewDidLoad]; 
 
self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, [[UIScreen mainScreen] bounds].size.width, 20)]; 
[self.button setTitle:@"  " forState:UIControlStateNormal]; 
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
[self.view addSubview:self.button]; 
 
[self.button addTarget:self action:@selector(clickMe:) forControlEvents:UIControlEventTouchUpInside]; 
 
} 
 
-(void)clickMe:(id)sender{ 
 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"  " message:@"      " delegate:self cancelButtonTitle:@"  " otherButtonTitles:nil, nil nil]; 
[alert show]; 
 
} 
 
@end  
ただし、警告‘UIAlertView’ is deprecated:first deprecated in iOS 9.0 - UIAlertView is deprecated.は、UIAlertViewがiOS 9で破棄された(推奨されていない)ことを示します.UIAlertControllerの使用を推奨します.
このwarningを解決するためにUIAlertControllerを用いてこの問題を解決した.コードは次のとおりです.
#import "ViewController.h" 
 
@interface ViewController () 
 
@property(strong,nonatomic) UIButton *button; 
 
@end 
 
@implementation ViewController 
 
- (void)viewDidLoad { 
[super viewDidLoad]; 
 
self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, [[UIScreen mainScreen] bounds].size.width, 20)]; 
[self.button setTitle:@"  " forState:UIControlStateNormal]; 
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
[self.view addSubview:self.button]; 
 
[self.button addTarget:self action:@selector(clickMe:) forControlEvents:UIControlEventTouchUpInside]; 
 
} 
 
-(void)clickMe:(id)sender{ 
 
//      ; 
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"  " message:@"      " preferredStyle: UIAlertControllerStyleAlert]; 
 
[alert addAction:[UIAlertAction actionWithTitle:@"  " style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 
//         ; 
}]]; 
 
//     ; 
[self presentViewController:alert animated:true completion:nil]; 
 
} 
@end 
実行することで、プログラムの実行後の効果は同じです.このうちpreferredStyleというパラメータには、UIAlertControllerStyleActionSheetという別の選択肢があります.この列挙タイプを選択すると、プロンプトボックスが下部からポップアップされます.
比較:コードを表示すると、プロンプトボックスのボタン応答はdelegate依頼を必要とせずに実現されることもわかります.addActionをそのまま使うと1つのblockでボタンクリックができるので便利です.