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

相关文章

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

SpringBoot3.4配置校验新特性的用法详解

《SpringBoot3.4配置校验新特性的用法详解》SpringBoot3.4对配置校验支持进行了全面升级,这篇文章为大家详细介绍了一下它们的具体使用,文中的示例代码讲解详细,感兴趣的小伙伴可以参考... 目录基本用法示例定义配置类配置 application.yml注入使用嵌套对象与集合元素深度校验开发

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

pandas中位数填充空值的实现示例

《pandas中位数填充空值的实现示例》中位数填充是一种简单而有效的方法,用于填充数据集中缺失的值,本文就来介绍一下pandas中位数填充空值的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是中位数填充?为什么选择中位数填充?示例数据结果分析完整代码总结在数据分析和机器学习过程中,处理缺失数

Pandas统计每行数据中的空值的方法示例

《Pandas统计每行数据中的空值的方法示例》处理缺失数据(NaN值)是一个非常常见的问题,本文主要介绍了Pandas统计每行数据中的空值的方法示例,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是空值?为什么要统计空值?准备工作创建示例数据统计每行空值数量进一步分析www.chinasem.cn处

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Java中的@SneakyThrows注解用法详解

《Java中的@SneakyThrows注解用法详解》:本文主要介绍Java中的@SneakyThrows注解用法的相关资料,Lombok的@SneakyThrows注解简化了Java方法中的异常... 目录前言一、@SneakyThrows 简介1.1 什么是 Lombok?二、@SneakyThrows

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http