iOS8下的UIAlertContoller初探

iOS8下的UIAlertContoller初探

iOS8推出了几个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,这不经意的改变,貌似把我之前理解的“controller”一下子推翻了~但是也无所谓,有新东西不怕,学会使用了就行。接下来会探讨一下这些个新的Controller。

 

- (void)showOkayCancelAlert {
    NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
    NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
    NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
    NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"The \"Okay/Cancel\" alert's other action occured.");
    }];
    
    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

这是最普通的一个alertcontroller,一个取消按钮,一个确定按钮。

新的alertcontroller,其初始化方法也不一样了,按钮响应方法绑定使用了block方式,有利有弊。需要注意的是不要因为block导致了引用循环,记得使用__weak,尤其是使用到self。

上面的界面如下:

iOS8下的UIAlertContoller初探

如果UIAlertAction *otherAction这种otherAction多几个的话,它会自动排列成如下:

iOS8下的UIAlertContoller初探

另外,很多时候,我们需要在alertcontroller中添加一个输入框,例如输入密码:

iOS8下的UIAlertContoller初探

这时候可以添加如下代码:

 [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        // 可以在这里对textfield进行定制,例如改变背景色
        textField.backgroundColor = [UIColor orangeColor];
    }];

而改变背景色会这样:

iOS8下的UIAlertContoller初探

完整的密码输入:

- (void)showSecureTextEntryAlert {
    NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
    NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
    NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
    NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    // Add the text field for the secure text entry.
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        // Listen for changes to the text field's text so that we can toggle the current
        // action's enabled property based on whether the user has entered a sufficiently
        // secure entry.
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];

        textField.secureTextEntry = YES;
    }];

    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"The \"Secure Text Entry\" alert's cancel action occured.");

        // Stop listening for text changed notifications.
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
    }];

    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"The \"Secure Text Entry\" alert's other action occured.");

        // Stop listening for text changed notifications.
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
    }];
    
    // The text field initially has no text in the text field, so we'll disable it.
    otherAction.enabled = NO;

    // Hold onto the secure text alert action to toggle the enabled/disabled state when the text changed.
    self.secureTextAlertAction = otherAction;

    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

注意四点:

1.添加通知,监听textfield内容的改变:

// Add the text field for the secure text entry.
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        // Listen for changes to the text field's text so that we can toggle the current
        // action's enabled property based on whether the user has entered a sufficiently
        // secure entry.
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];

        textField.secureTextEntry = YES;
    }];

2.初始化时候,禁用“ok”按钮:

otherAction.enabled = NO;

self.secureTextAlertAction = otherAction;//定义一个全局变量来存储

3.当输入超过5个字符时候,使self.secureTextAlertAction = YES:

- (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification {
    UITextField *textField = notification.object;

    // Enforce a minimum length of >= 5 characters for secure text alerts.
    self.secureTextAlertAction.enabled = textField.text.length >= 5;
}

4.在“OK”action中去掉通知:

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"The \"Secure Text Entry\" alert's other action occured.");

        // Stop listening for text changed notifications.
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
    }];

 

最后是以前经常是alertview与actionsheet结合使用,这里同样也有:

- (void)showOkayCancelActionSheet {
    NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
    NSString *destructiveButtonTitle = NSLocalizedString(@"OK", nil);
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"The \"Okay/Cancel\" alert action sheet's cancel action occured.");
    }];
    
    UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"The \"Okay/Cancel\" alert action sheet's destructive action occured.");
    }];
    
    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:destructiveAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

在底部显示如下:

iOS8下的UIAlertContoller初探

 

好了,至此,基本就知道这个新的controller到底是怎样使用了。

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/nathanou/p/3778200.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109807.html原文链接:https://javaforall.net

(0)
上一篇 2021年8月29日 下午10:00
下一篇 2021年8月29日 下午11:00


相关推荐

  • impala实战篇

    impala实战篇第 1 章 impala 基本概念 1 什么是 impalaCloude 公司推出 提供对 HDFS Hbase 数据的高性能 低延迟的交互式 SQL 查询功能 基于 Hive 使用内存计算 兼顾数据仓库 具有实时 批处理 多并发等优点 是 CDH 平台首选的 PB 级大数据实时查询分析引擎 1 1Impala 的优缺点 1 1 1 优点基于内存运算 不需要把中间结果写入磁盘 省掉了大量的 I O 开销 无需转换 MapReduce 直接访问存储在 HDFS HBase 中的数据进行作业调度 速度快 使用了支持

    2025年9月3日
    5
  • 04_hadoop集群的集中管理

    04_hadoop集群的集中管理

    2021年8月22日
    66
  • phpstorm激活码2021.3月最新在线激活

    第1章 Django入门到进阶-更适合Python小白的系统课程课程简介和开发环境配置~第2章 Django中的路由与视图本章主要讲解Django中视图和路由器的创建,并深入讲解路由器中地址的参数定义phpstorm激活码20213月最新在线激活,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月13日
    43
  • 最小角回归算法定义_有无回归算法

    最小角回归算法定义_有无回归算法最小角回归算法(LeastAngleRegression,LAR)是一种针对于线性回归问题,快速进行特征选择和回归系数计算的迭代算法,其被广泛推广用于求解线性回归以及Lasso回归问题。最小角回归算法的核心思想为:将回归目标向量依次分解为若干组特征向量的线性组合,最终使得与所有特征均线性无关的残差向量最小。可见,最小角回归算法的关键在于选择正确的特征向量分解顺序和分解系数。为了更好的表示最…

    2022年8月21日
    9
  • RSA登录加密_rsa私钥加密公钥解密

    RSA登录加密_rsa私钥加密公钥解密随手记2本文章仅作学习参考使用,不做其他使用。​​​​​​网站:aHR0cHM6Ly9iZWlqaW5nLnR1aXR1aTk5LmNvbS9kZW5nbHUuaHRtbA==输入登录密码“123456”,分析抓包数据如下:返回了一个document类型的包,表单提交的方式,无法使用跟栈的方式定位加密方法,所以这里我使用搜索url的方式定位加密位置,如下:然后在全局搜索关键字“l_submit”,直接跟进加密方法里去,下断点开始调试得到了密码的明文数据,并且在下面也发…

    2025年8月26日
    6
  • 升级node到指定版本

    升级node到指定版本查看 node 版本 node vmac 系统 1 清除 npm 缓存 执行命令 npmcacheclea f2 n 模块是专门用来管理 nodejs 的版本 安装 n 模块 npminstall gn3 更新升级 node 版本 nstable 把当前系统的 Node 更新成最新的 稳定版本 nlts 长期支持版 nlatest 最新版 n10 14 2 指定安装版本 4 查看升级后的 node 版本 node vwindow

    2026年3月17日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号