iOS开发UIAlertController常用属性方法

UIAlertController是iOS8推出的新概念,取代了之前的 UIAlertView和UIActionSheet(虽然现在仍可以使用,但是会有警告)。所以还是建议大家以后还是使用UIAlertController吧(本人看见警告就有种冲动?),下面有些代码有点重复,但是能过为了更清楚的看到效果

//

//  ViewController.m

//  UIAlertControllerAll

#import "ViewController.h"

 

@interface ViewController ()<UIAlertViewDelegate>

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];

    

    //点击弹出alert的按钮

    UIButton *First_Button = [UIButton buttonWithType:UIButtonTypeCustom];

    First_Button.frame = CGRectMake(30, 50, 100, 40);

    [First_Button setBackgroundColor:[UIColor brownColor]];

    [First_Button setTitle:@"标准Alert" forState:UIControlStateNormal];

    [First_Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    First_Button.tag = 111;

    [self.view addSubview:First_Button];

    

    UIButton *Second_Button = [UIButton buttonWithType:UIButtonTypeCustom];

    Second_Button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 130, 50, 100, 40);

    [Second_Button setBackgroundColor:[UIColor brownColor]];

    [Second_Button setTitle:@"标准Sheet" forState:UIControlStateNormal];

    [Second_Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    Second_Button.tag = 222;

    [self.view addSubview:Second_Button];

    

    UIButton *Third_Button = [UIButton buttonWithType:UIButtonTypeCustom];

    Third_Button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2.0 - 50, 140, 100, 40);

    [Third_Button setBackgroundColor:[UIColor brownColor]];

    [Third_Button setTitle:@"多按钮操作" forState:UIControlStateNormal];

    [Third_Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    Third_Button.tag = 333;

    [self.view addSubview:Third_Button];

    

    

}

- (void)buttonAction:(UIButton *)button

{

    if (button.tag == 111) {

        NSLog(@"Alert");

        //UIAlertController风格:UIAlertControllerStyleAlert

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"没有标题的标题"

                                                                                 message:@"学无止境,漫漫长路"

                                                                          preferredStyle:UIAlertControllerStyleAlert ];

        

        //添加取消到UIAlertController中

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"Alert 取消");

        }];

        [alertController addAction:cancelAction];

        

        //添加确定到UIAlertController中

        UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"Alert 确定");

        }];

        [alertController addAction:OKAction];

        

        __weak __typeof(self) weakSelf = self;

        

        //添加输入框

        [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

            textField.placeholder = @"美女芳名啊?";

            [[NSNotificationCenter defaultCenter]addObserver:weakSelf selector:@selector(TextFieldDidChanged:) name:UITextFieldTextDidChangeNotification object:nil];

            

            //UIKIT_EXTERN NSNotificationName const UITextFieldTextDidBeginEditingNotification;

            //UIKIT_EXTERN NSNotificationName const UITextFieldTextDidEndEditingNotification;

            //UIKIT_EXTERN NSNotificationName const UITextFieldTextDidChangeNotification;

            

        }];

        // 添加密码输入框

        [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

            textField.placeholder = @"请输入密码";

            //这里还可以进行多种设置

            [textField setSecureTextEntry:YES];//设置成隐号形式

        }];

        

        [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

            textField.placeholder = @"不行啊";

            

        }];

        

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

        

    }else if(button.tag == 222){

        NSLog(@"Sheet");

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标准的Action Sheet样式"

                                                                                 message:nil

                                                                          preferredStyle:UIAlertControllerStyleActionSheet];

        //取消:style:UIAlertActionStyleCancel

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"Sheet 取消");

        }];

        [alertController addAction:cancelAction];

        //了解更多:style:UIAlertActionStyleDestructive

        UIAlertAction *moreAction = [UIAlertAction actionWithTitle:@"了解更多" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"Sheet 了解更多");

        }];

        [alertController addAction:moreAction];

        //原来如此:style:UIAlertActionStyleDefault

        UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"原来如此" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"Sheet 原来如此");

        }];

        [alertController addAction:OKAction];

        

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

        

    }else if (button.tag == 333){

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"你猜我选哪个呢?"

                                                                                 message:@"美女就是多"

                                                                          preferredStyle:UIAlertControllerStyleActionSheet ];

        

        UIAlertAction *home1Action = [UIAlertAction actionWithTitle:@"腿长" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"多按钮 腿长");

        }];

        [alertController addAction:home1Action];

        UIAlertAction *home2Action = [UIAlertAction actionWithTitle:@"漂亮" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"多按钮 漂亮");

        }];

        [alertController addAction:home2Action];

        UIAlertAction *home3Action = [UIAlertAction actionWithTitle:@"女强人" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"多按钮 女强人");

        }];

        

        [alertController addAction:home3Action];

        UIAlertAction *home4Action = [UIAlertAction actionWithTitle:@"土豪" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"多按钮 土豪");

        }];

        [alertController addAction:home4Action];

        //取消style:UIAlertActionStyleDefault

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"全带走" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"多按钮 全带走");

        }];

        [alertController addAction:cancelAction];

        

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

        

    }

    /*

     style:UIAlertActionStyleDefault//对按钮应用标准样式

     style:UIAlertActionStyleCancel//对按钮应用取消样式,即取消操作

     style:UIAlertActionStyleDestructive//对按钮应用警示性样式,提示用户这样做可能会删除或者改变某些数据

     */

    

}

 

- (void)TextFieldDidChanged:(NSNotification *)notification {

    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;

    if (alertController) {

        UITextField *First_TextField = alertController.textFields.firstObject;

        UIAlertAction *action  = alertController.actions.lastObject;

        action.enabled = First_TextField.text.length > 0;

        NSLog(@"%@", First_TextField.text);

        

        UITextField *Second_TextField = alertController.textFields[1];

        NSLog(@"%@", Second_TextField.text);

        

        UITextField *Third_TextField = alertController.textFields[2];

        NSLog(@"%@", Third_TextField.text);

    }

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

@end

 

转载于:https://www.cnblogs.com/freeleader/p/7411889.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值