IOS UIAlertController 彈框 (ios 9.0 後代替了UIAlertView彈框 和 UIActionSheet下彈框)

[摘要:正在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];
}

這裏寫圖片描述
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章