UIAlertController(警告栏) 自学之初体验

UIAlertController有两种样式  preferredStyle:

UIAlertControllerStyleAlert (位于屏幕的中部)

UIAlertControllerStyleActionSheet(位于屏幕的下方)
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"默罕默德~本拉登" preferredStyle:UIAlertControllerStyleAlert];  //UIAlertController的创建

UIAlertAction是UIAlertController的按钮样式
title按钮的名称,style按钮的样式,handler处理层序(点击按钮执行的代码)
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消cancel" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"默认" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:nil];
    //添加按钮到UIAlertController上
    [alert addAction:cancelAction];
    [alert addAction:defaultAction];
    [alert addAction:resetAction];

 1、文本输入框只能添加到Alert的风格中,ActionSheet是不允许的;

 2、UIAlertController具有只读属性的textFields数组,需要可直接按自己需要的顺序添加;

 3、添加方式是使用block,参数是UITextField;

 4、添加UITextField监听方法和实现方法。

    //添加文本输入框
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"登陆";
        //可以为textField添加事件
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"密码";
        textField.secureTextEntry = YES;
        //可以为textField添加事件
    }];

添加一个事件可以用来输出用户名和密码(textFields是属性,是一个数组)

UIAlertAction *getAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
            UITextField *login = alert.textFields[0];
            UITextField *passWord = alert.textFields[1];
            NSLog(@"登陆:%@   密码:%@",login.text,passWord.text);
        }];//获取textField的文本内容
        [alert addAction:getAction];


如果要监听textField开始,结束,改变状态,需要添加监听代码
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
            textField.placeholder = @"添加监听代码";
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
        }];
    [self presentViewController:alert animated:YES completion:^{
    }];  //模态推送到页面上

//监听的方法
-(void)alertTextFieldDidChange:(NSNotification *)notification{
    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
    if (alertController) {
        //为textFields数组中下标为2的textField为监听对象
        //也是alertController.textFields.lastObject
        UITextField *listen = alertController.textFields[2];
        //限制,如果listen限制输入长度在5个字符内,否则不允许点击默认Defult键
        //当UITextField输入字数超过5个,按钮变灰色,enable为NO
        UIAlertAction *action = alertController.actions.lastObject;
        action.enabled = listen.text.length<=5;
    }

 

转载于:https://www.cnblogs.com/LE-Quan/p/5145566.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值