iOS个人整理17-警示框--UIAlertController

一、UIAlertController


alert顾名思义是弹出一个提示框,在某个版本以前一直用的是UIAlertView,它继承的是UIView,现在已经废弃,但仍可以使用

UIAlertController完全取代了UIAlertView,因为继承于UIViewController,理论上功能更强大一点


一般情况下,UIAlertController是点击某个按钮后弹出的,我们就可以把UIAlertController的创建写在某个Button的点击方法中

在某个button的点击方法中,我们这样写:


1.使用时先初始化,title是显示的标题,message是标题下面的字,详细说明,

preferredStyle是样式有两种

UIAlertControllerStyleAlert:中间弹出的提示框

UIAlertControllerStyleActionSheet:底部弹出的提示框

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这个是UIAlertController的默认样式" preferredStyle:UIAlertControllerStyleAlert];

2.初始化完毕后警示框是没有按钮的,需要单独添加按钮UIAlertAction

首先初始化一个按钮

按钮的style有三种:

UIAlertActionStyleDefault:默认样式

UIAlertActionStyleCancel:取消按钮

UIAlertActionStyleDestructive:红字的警示按钮

按钮的handler属性后面可以跟一个block,用来传值

    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
  //把alert窗口用模态方法弹出
    [self presentViewController:alertController animated:YES completion:nil];
    
3.再将按钮添加到警示框上面去,把警示框用模态的方法添加到父视图

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


4.在UIAlertController中我们不但可以添加UIAlertAction按钮,还可以添加文本框

一般不常用,实现起来一些具体问题也有点绕

先初始化一个UIAlertController,使用addTextFieldWithConfigurationHandler方法添加textField,这个方法后面跟的参数是block,block带一个UITextField的参数

这个textField就是添加上去的文本框,可以对它的属性进行设置

UIAlertController *alertController = 
[UIAlertController alertControllerWithTitle:@"文本对话框" message:@"登录和密码对话框示例" preferredStyle:UIAlertControllerStyleAlert];

 [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.text = @"asda";
    textField.backgroundColor = [UIColor colorWithRed:109/255.0 green:211/255.0 blue:206/255.0 alpha:1];
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.textAlignment = NSTextAlignmentCenter;
 }];

 

这里可以添加一个方法,对textField上的文字输入进行限制,少于5字符不能点击按钮,

在这个block中声明一个通知中心,当UITextFieldTextDidChangeNotification发生的时候(也就是textField的内容改变时)

进入方法验证输入长度够不够

     
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){

//NSNotificationCenter defaultCenter通知一件事的改变会触发另一个事件

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];

 textField.placeholder = @"登录"; }];

 实现一个方法根据textField输入长度是否够,控制按钮是否可点击 

- (void)alertTextFieldDidChange:(NSNotification *)notification{
    //得到当前推送的AlertViewController
    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
    if (alertController) {
        //获得AlertController上的文本框
        UITextField *login = alertController.textFields.firstObject;
        UIAlertAction *okAction = alertController.actions.lastObject;
        okAction.enabled = login.text.length > 5;
    }
}



下面我们展示几个实例

这是UIAlertControllerStyleAlert类型的,上面添加了一个cancel按钮和一个destruct按钮



这是UIAlertControllerStyleActionSheet的样子



这是添加了文本框的样子


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值