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

相关文章

HTML5的input标签的`type`属性值详解和代码示例

《HTML5的input标签的`type`属性值详解和代码示例》HTML5的`input`标签提供了多种`type`属性值,用于创建不同类型的输入控件,满足用户输入的多样化需求,从文本输入、密码输入、... 目录一、引言二、文本类输入类型2.1 text2.2 password2.3 textarea(严格

MySQL中between and的基本用法、范围查询示例详解

《MySQL中betweenand的基本用法、范围查询示例详解》BETWEENAND操作符在MySQL中用于选择在两个值之间的数据,包括边界值,它支持数值和日期类型,示例展示了如何使用BETWEEN... 目录一、between and语法二、使用示例2.1、betwphpeen and数值查询2.2、be

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Java数组动态扩容的实现示例

《Java数组动态扩容的实现示例》本文主要介绍了Java数组动态扩容的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1 问题2 方法3 结语1 问题实现动态的给数组添加元素效果,实现对数组扩容,原始数组使用静态分配

JAVA项目swing转javafx语法规则以及示例代码

《JAVA项目swing转javafx语法规则以及示例代码》:本文主要介绍JAVA项目swing转javafx语法规则以及示例代码的相关资料,文中详细讲解了主类继承、窗口创建、布局管理、控件替换、... 目录最常用的“一行换一行”速查表(直接全局替换)实际转换示例(JFramejs → JavaFX)迁移建

MyBatis中的两种参数传递类型详解(示例代码)

《MyBatis中的两种参数传递类型详解(示例代码)》文章介绍了MyBatis中传递多个参数的两种方式,使用Map和使用@Param注解或封装POJO,Map方式适用于动态、不固定的参数,但可读性和安... 目录✅ android方式一:使用Map<String, Object>✅ 方式二:使用@Param

SpringBoot实现图形验证码的示例代码

《SpringBoot实现图形验证码的示例代码》验证码的实现方式有很多,可以由前端实现,也可以由后端进行实现,也有很多的插件和工具包可以使用,在这里,我们使用Hutool提供的小工具实现,本文介绍Sp... 目录项目创建前端代码实现约定前后端交互接口需求分析接口定义Hutool工具实现服务器端代码引入依赖获

C#中DateTime的格式符的实现示例

《C#中DateTime的格式符的实现示例》本文介绍了C#中DateTime格式符的使用方法,分为预定义格式和自定义格式两类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录DateTime的格式符1.核心概念2.预定义格式(快捷方案,直接复用)3.自定义格式(灵活可控

MyBatisPlus乐观锁和悲观锁的实现示例

《MyBatisPlus乐观锁和悲观锁的实现示例》本文主要介绍了MyBatisPlus乐观锁和悲观锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小... 目录1.场景2.乐观锁和悲观锁3.乐观锁实现4.悲观锁1.场景一件商品,成本价是80元,售价是10

Java中@Accessors使用的实现示例

《Java中@Accessors使用的实现示例》本文主要介绍了Java中@Accessors使用的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、@Accessors(chain = true)二、@Accessors(fluent =