IOS UIAlertController 彈框 (ios 9.0 後代替了UIAlertView彈框 和 UIActionSheet下彈框)
阿新 • • 發佈:2018-11-11
[摘要:正在IOS 9.0 後 蘋果沒有 UIActionSheet 由UIAlertController取代二者 用操縱器將二者開兩為一 很簡略 輕易]
在IOS 9.0 後 蘋果官方宣佈不再或不推薦使用UIAlertView 和 UIActionSheet 由UIAlertController進行代替兩者 用控制器將兩者合二為一 很簡單 方便 下面就是關於UIAlertView的常用方法
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 建立一個BUTTON 點選顯示彈框
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
button.frame = CGRectMake(100, 100, 100, 100);
// 給BUTTON 新增點選方法
[button addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchUpInside)];
button.backgroundColor = [UIColor blueColor];
[self .view addSubview:button];
}
// button的點選方法
- (void)actionButton:(UIButton *)button
{
// 初始化一個一個UIAlertController
// 引數preferredStyle:是IAlertController的樣式
// UIAlertControllerStyleAlert 創建出來相當於UIAlertView
// UIAlertControllerStyleActionSheet 創建出來相當於 UIActionSheet
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"靜" preferredStyle:(UIAlertControllerStyleAlert)];
// 建立按鈕
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
NSLog(@"注意學習");
}];
// 建立按鈕
// 注意取消按鈕只能新增一個
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action) {
// 點選按鈕後的方法直接在這裡面寫
NSLog(@"注意學習");
}];
// // 建立警告按鈕
// UIAlertAction *structlAction = [UIAlertAction actionWithTitle:@"警告" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction *action) {
// NSLog(@"注意學習");
// }];
//
// 新增按鈕 將按鈕新增到UIAlertController物件上
[alertController addAction:okAction];
[alertController addAction:cancelAction];
//[alertController addAction:structlAction];
// 只有在alert情況下才可以新增文字框
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"使用者名稱";
textField.secureTextEntry = YES;
}];
// // 取出文字
// UITextField *text = alertController.textFields.firstObject;
// UIAlertAction *action = alertController.actions.firstObject;
// 將UIAlertController模態出來 相當於UIAlertView show 的方法
[self presentViewController:alertController animated:YES completion:nil];
}