UIAlertController的用法示例

2023-10-13 13:58

本文主要是介绍UIAlertController的用法示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在iOS8中 我们熟悉的UIAlertView已经不被苹果提倡了 取而代之的是UIAlertController  关于详细的解释说明上一篇转载的文章里  已经很详细的介绍了  这篇文章 主要来自己敲一下代码 顺便看一下实现的效果有什么不同

 

//    1.Alert

    UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:nilmessage:@"确定要注销吗"preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {

        [self.navigationControllerpopToRootViewControllerAnimated:YES];

    }];

    UIAlertAction *cancleAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

    [controller addAction:okAction];

    [controller addAction:cancleAction];

    [selfpresentViewController:controller animated:YEScompletion:nil];

   实现的效果是这样的:



//    2.ActionSheet

    UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:@"Tips"message:@"确定要注销吗"preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {

        [self.navigationControllerpopToRootViewControllerAnimated:YES];

    }];

    UIAlertAction *cancleAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

    [controller addAction:okAction];

    [controller addAction:cancleAction];

    [selfpresentViewController:controller animated:YEScompletion:nil];


实现效果是这样的:


 //3.textfield

   #pragma 注意 如果想加入textfield 类型只能是StyleAlert

    UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:@"Tips"message:@"请输入用户名密码"preferredStyle:UIAlertControllerStyleAlert];

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

        textField.placeholder  = @"用户名";

    }];

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

        textField.placeholder = @"密码";

        textField.secureTextEntry = YES;

    }];

    UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {

        [self.navigationControllerpopToRootViewControllerAnimated:YES];

    }];

    UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

    

    

    [controller addAction:okAction];

    [controller addAction:cancelAction];

    [selfpresentViewController:controller animated:YEScompletion:nil];

  实现效果是这样的:


最后加上对textfield用户名长度判断处理的代码

- (IBAction)logout:(UIBarButtonItem *)sender {

   #pragma 注意 如果想加入textfield 类型只能是StyleAlert

    controller = [UIAlertController alertControllerWithTitle:@"Tips" message:@"请输入用户名密码" preferredStyle:UIAlertControllerStyleAlert];

    

    /*增加一个监听*/

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

        textField.placeholder  = @"用户名(长度至少为3)";

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

    }];

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

        textField.placeholder = @"密码";

        textField.secureTextEntry = YES;

    }];

        /*当确定按钮按下时  读取*/

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

    [[NSNotificationCenter defaultCenter]removeObserver:self];

    }];

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

        [self.navigationController popToRootViewControllerAnimated:YES];

    }];

    [controller addAction:okAction];

    [controller addAction:cancelAction];

    okAction.enabled = NO;

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

}

- (void)usernameChanged{

    okAction.enabled = controller.textFields.firstObject.text.length >= 3;

}


实现效果是这样的:

长度没有3位:


如果长度达到了3位 显示效果是这样的

这篇关于UIAlertController的用法示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/203695

相关文章

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477