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来获取到输入框,用于后续操作。

    
    
    
  • 相关阅读:
    CAN总线学习资料
    VMware虚拟机 硬盘空间不足 磁盘大小调整方案
    郭天祥-S3C2440开发板Linux2.6.31移植教程
    MFC串口编程——使用标准SerialCom类
    Luogu P2602 [ZJOI2010]数字计数 //数位DP
    luogu P1896 [SCOI2005] 互不侵犯 //状压DP
    中北大学ACM 5/12 T6 CSY的幸福
    P2473 || SCOI2008 奖励关 //状压&&期望DP
    请让蝴蝶爬满全身
    【图论】二分图 // 未完成 =、=
  • 原文地址:https://www.cnblogs.com/PaulpauL/p/6106535.html
Copyright © 2011-2022 走看看