zoukankan      html  css  js  c++  java
  • UIAlertViewController的使用

    UIAlertViewController是苹果自带的信息提示框,仅在iOS8.0以后可以使用

    NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
    

    下面是一个使用的简单例子:

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDestructive handler:nil];
        [alertController addAction:cancelAction];
        [alertController addAction:okAction];
    
        [self presentViewController:alertController animated:YES completion:nil];

    UIAlertController的alertControllerWithTitle:message:preferredStyle:方法中有个样式参数:preferredStyle

    此参数为枚举类型:

    typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
        UIAlertControllerStyleActionSheet = 0,
        UIAlertControllerStyleAlert
    } NS_ENUM_AVAILABLE_IOS(8_0);
    UIAlertControllerStyleAlert:该样式与UIAlertView的样式一样,显示在屏幕中央。
    UIAlertControllerStyleActionSheet:该样式显示在屏幕底部,自下而上推出。

    UIAlertAction的actionWithTitle:style:handler:方法中也有一个样式参数叫做:style
    此参数为枚举类型:
    typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
        UIAlertActionStyleDefault = 0,
        UIAlertActionStyleCancel,
        UIAlertActionStyleDestructive
    } NS_ENUM_AVAILABLE_IOS(8_0);
    
    
    UIAlertActionStyleDefault是默认样式,白底黑字,响应touchUpInside事件。
    UIAlertActionStyleCancel是取消样式,与UIAlertActionStyleDefault唯一的不同就是它的字体加粗了。
    UIAlertActionStyleDestructive是具有警示性的样式,与UIAlertActionStyleDefault唯一的不同就是它的字体变为红色了。

    有个问题:UIAlertController只能显示提示信息吗?如果是的话,为什么不继续使用UIAlertView?如果不是,那它还可以做什么?

    答:不是。UIAlertController还可以添加文本输入框,与用户进行文本信息输入交互,而不再仅仅是输出给用户提示信息。接下来看一下一个简单的例子:


    首先,将alertController声明为全局对象便于在整个类中使用

    @interface ViewController ()
    
    @property (nonatomic, strong) UIAlertController *alertController;
    
    @end
    

      

    接下来是实例化alertController

        self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
        
        [self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
            textField.placeholder = @"登录";
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertViewTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
        }];
        [self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
            textField.placeholder = @"密码";
            textField.secureTextEntry = YES;
        }];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
        [okAction setEnabled:NO];
        
        [self.alertController addAction:cancelAction];
        [self.alertController addAction:okAction];
    
        [self presentViewController:self.alertController animated:YES completion:nil];
    

      

    较之前面的代码,这里多了两行代码:

        [self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
            textField.placeholder = @"登录";
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertViewTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
        }];
        [self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
            textField.placeholder = @"密码";
            textField.secureTextEntry = YES;
        }];
    

      

    这两行代码就是在alertController中加入文本输入框,让用户输入必要信息,以便交互。其中给textField设置了placeholder和secureTextEntry,另外还添加了属性监听。

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

      

    此处的监听事件会在文本框变化时触发,方法如下:

    -(void)alertViewTextFieldDidChange:(NSNotification *)notification
    {
        UITextField *textField = notification.object;
        UIAlertAction *action = self.alertController.actions[1];
        [action setEnabled:NO];
        if(textField.text.length>3)
        {
            NSLog(@"textField.text = %@",textField.text);
            [action setEnabled:YES];
        }
    }
    

      

    方法中通过:notification.object来获取到输入框,用于后续操作。

    
    
    
  • 相关阅读:
    Sentinel Dashboard(基于1.8.1)流控规则持久化到Nacos——涉及部分Sentinel Dashboard源码改造
    测试平台MeterSphere源码入门
    Java:利用BigDecimal类巧妙处理Double类型精度丢失
    SpringBoot整合任务调度框架Quartz及持久化配置
    任务调度框架Quartz快速入门!
    Kafka超详细学习笔记【概念理解,安装配置】
    Windows环境下Zookeeper安装配置
    SpringData JPA利用Specification多条件查询
    SpringBoot事件监听机制及观察者模式/发布订阅模式
    详解Java中的IO输入输出流!
  • 原文地址:https://www.cnblogs.com/PaulpauL/p/6106535.html
Copyright © 2011-2022 走看看