iOS:UIAlertController和UIAlertAction的詳解


提示框控制器:UIAlertController

提示框按鈕:UIAlertAction
 
功能:用來提示信息,並給出一些可以進行選擇的按鈕來處理相應的要求。
 
注意: 在Xcode的iOS8 SDK中,UIAlertView和UIActionSheet都被UIAlertController取代。官方庫解釋: “UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.”、“UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead.” 。說明了在iOS8+開發,UIALertView和UIActionSheet已經過時了,UIAlertController以一種模塊化替換的方式來代替這兩這兩個控件的功能和作用。如何創建及使用UIAlertController成為我們所關注的問題。
 
類介紹:
1、提示框風格枚舉(分為UIAlertView、UIActionSheet)

typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {

    UIAlertControllerStyleActionSheet = 0,  //在視圖底部彈出的提示框,它不能添加文本框,而且在ipad中必須使用popover形式展示

    UIAlertControllerStyleAlert                  //在視圖中間彈出的提示框

} NS_ENUM_AVAILABLE_IOS(8_0);

 

2、提示框上按鈕的風格

typedef NS_ENUM(NSInteger, UIAlertActionStyle) {

    UIAlertActionStyleDefault = 0,    //默認的確認按鈕

    UIAlertActionStyleCancel,          //默認的取消按鈕

    UIAlertActionStyleDestructive    //默認的紅色按鈕

}NS_ENUM_AVAILABLE_IOS(8_0);

 

3、UIAlertController:提示框控制器類

@interface UIAlertController : UIViewController

方法:

//創建提示框控制器的類方法

+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;

//在提示框上添加文本框的實例方法(只能在UIAlertView風格的提示框添加)

- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;

//在提示框上添加按鈕

- (void)addAction:(UIAlertAction *)action;

 

屬性:

//提示框上存放所有按鈕的數組

@property (nonatomic, readonly) NSArray *actions;

//提示框上存放所有文本框的數組

@property (nonatomic, readonly) NSArray *textFields;

//提示框的標題

@property (nonatomic, copy) NSString *title;

//提示信息

@property (nonatomic, copy) NSString *message;

//提示框控制器的風格

@property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;

@end

 

4、UIAlertAction:提示框按鈕

@interface UIAlertAction : NSObject <NSCopying>

方法:

//創建提示框按鈕的類方法

+ (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(UIAlertAction *action))handler;

 

屬性:

//按鈕標題

@property (nonatomic, readonly) NSString *title;

//按鈕的風格

@property (nonatomic, readonly) UIAlertActionStyle style;

//按鈕是否有效

@property (nonatomic, getter=isEnabled) BOOL enabled;

@end

 

具體的實例如下:

創建步驟:

1、布局故事板,在控制器的視圖中拖入一個按鈕,並關聯IBAction事件

 

2、在按鈕的關聯事件中的主要代碼如下:

//創建提示框控制器

    //創建提示框控制器
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示框" message:@"消息" preferredStyle:UIAlertControllerStyleAlert];
    alertController.view.backgroundColor = [UIColor purpleColor];

//創建提示框按鈕

    //創建提示按鈕
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"默認Cancel" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"默認Default" style:UIAlertActionStyleDefault handler:nil];
    
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"默認Destructive" style:UIAlertActionStyleDestructive handler:nil];

//添加提示按鈕到提示框中

    //添加提示按鈕
    [alertController addAction:action1];
    [alertController addAction:action2];
    [alertController addAction:action3];

//添加文本框到提示框中(只適合提示框風格為:UIAlertControllerStyleAlert)

    //添加文本框(只適合alertview類型的提示框)
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"賬號";
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"密碼";
        textField.secureTextEntry = YES; //安全輸入模式
    }];

//給文本框添加監聽事件

    //給文本框添加監聽事件(文本框的開始、結束、狀態改變等)
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"添加監聽事件";
        
        [textField addTarget:self action:@selector(alertTextFiledDidChanged:) forControlEvents:UIControlEventEditingChanged];
    }];

//以模態窗口的形式顯示提示框

[self presentViewController:alertController animated:YES completion:nil];

//實現文本框事件

#pragma mark 文本框監聽事件
-(void)alertTextFiledDidChanged:(NSNotification *)notification
{
    NSLog(@"Enditing changed");
}

 

//點擊按鈕,顯示演示結果

當沒有添加action3按鈕到提示框,即按鈕個數<=2時,兩種提示框的樣式截圖為:

UIAlertControllerStyleAlert:從屏幕中間彈出

 

UIAlertControllerStyleActionSheet:從屏幕底部彈出(不能添加文本框)

 

 

當沒有添加action3按鈕到提示框,即按鈕個數>=3時,兩種提示框的樣式截圖為:

UIAlertControllerStyleAlert:從屏幕中間彈出

UIAlertControllerStyleActionSheet:從屏幕底部彈出(不能添加文本框)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM