UIAlertController、UIAlertAction 警告框


 
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertAction : NSObject <NSCopying>

//創建操作
+ (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;

NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController

//初始化
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;

//添加操作
- (void)addAction:(UIAlertAction *)action;

示例1:最簡單的提醒視圖

//創建操作
    UIAlertAction *okAlert = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
     //具體操作內容
}];
//初始化
    UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"頂部標題欄"message:@"中間信息"preferredStyle:UIAlertControllerStyleAlert];
//添加操作
    [alert addAction:okAlert];
//以model形式,顯示警告視圖
    [self presentViewController:alert animated:YES completion:nil];

// 等同於
    NSString *title = @"頂部標題欄";
    NSString *message = @"中間信息";
    NSString *okAlertButton = @"OK";
    UIAlertAction *okAlert = [UIAlertAction
actionWithTitle:okAlertButton style:UIAlertActionStyleDefaulthandler:^(UIAlertAction * _Nonnull action) {

    }];

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

示例2:多個按鍵的提醒視圖

    //取消按鍵——下次下次 UIAlertActionStyleCancel   (粗體)
    [alert addAction:[UIAlertAction actionWithTitle:@"下次下次" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"點擊取消按鈕");
    }]];

      //紅色按鍵——殘忍拒絕 UIAlertActionStyleDestructive
    [alert addAction:[UIAlertAction actionWithTitle:@"殘忍拒絕" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"紅色按鈕");
    }]];

// 兩個會是這樣

// 3個就挨個往下排
    //普通按鍵——立馬好評 UIAlertActionStyleDefault

    [alert addAction:[UIAlertAction actionWithTitle:@"立馬好評" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"普通按鈕");
    }]];


示例3:帶對話框TextField

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

     //添加TextField 條欄  addTextFieldWithConfigurationHandler
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        NSLog(@"TextField");
        //中間的提示輸入
        textField.placeholder = @"用來提示你做什么事的";
    }];

示例4:提醒圖標,在最底部呈現 Sheet

// UIAlertControllerStyleAlert改成UIAlertControllerStyleActionSheet (最后面的)
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertControllerStyleActionsheet好像不能和TextField在一起,會報錯。是這樣嗎?

疑問:排列沒有次序?
用英文@“ok” 之類的,就會排列和我寫的順序一樣。

 

NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertAction : NSObject <NSCopying>
 
//創建操作
+ (
instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;
 
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
 
//初始化
+ ( instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
 
//添加操作
- ( void)addAction:(UIAlertAction *)action;
 
 
示例1:最簡單的提醒視圖
 
//創建操作
    UIAlertAction *okAlert = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
     //具體操作內容
}];
//初始化
    UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"頂部標題欄"message:@"中間信息"preferredStyle:UIAlertControllerStyleAlert];
//添加操作
    [alert addAction:okAlert];
//以model形式,顯示警告視圖
    [self presentViewController:alert animated:YES completion:nil];
 
 
// 等同於
    NSString *title = @"頂部標題欄";
    NSString *message = @"中間信息";
    NSString *okAlertButton = @"OK";
    UIAlertAction *okAlert = [UIAlertAction
actionWithTitle:okAlertButton style:UIAlertActionStyleDefaulthandler:^(UIAlertAction * _Nonnull action) {
       
    }];
 
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
 
 
示例2:多個按鍵的提醒視圖
 
    //取消按鍵——下次下次 UIAlertActionStyleCancel    (粗體)
    [alert addAction:[UIAlertAction actionWithTitle:@"下次下次" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"點擊取消按鈕");
    }]];
 
      //紅色按鍵——殘忍拒絕 UIAlertActionStyleDestructive  
    [alert addAction:[UIAlertAction actionWithTitle:@"殘忍拒絕" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"紅色按鈕");
    }]];
 
// 兩個會是這樣
 
 
// 3個就挨個往下排 
    //普通按鍵——立馬好評 UIAlertActionStyleDefault  
    [alert addAction:[UIAlertAction actionWithTitle:@"立馬好評" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"普通按鈕");
    }]];
 
 
 
示例3:帶對話框TextField
 
- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
 
     //添加TextField 條欄  addTextFieldWithConfigurationHandler 
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        NSLog(@"TextField");
        //中間的提示輸入
        textField.placeholder = @"用來提示你做什么事的";
    }];
 
 
示例4:提醒圖標,在最底部呈現 Sheet
 
// UIAlertControllerStyleAlert改成UIAlertControllerStyleActionSheet (最后面的)
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
 
 
UIAlertControllerStyleActionsheet好像不能和TextField在一起,會報錯。是這樣嗎?
 
疑問:排列沒有次序?
用英文@“ok” 之類的,就會排列和我寫的順序一樣。
 
 


免責聲明!

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



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