iOS - UIAlertController

简介: 前言 NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController @available(iOS 8.

前言

    NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
    @available(iOS 8.0, *)       public class UIAlertController : UIViewController

1、alertController 的创建

  • Objective-C

        // 1. 创建时不添加按钮
    
            // 实例化 alertController 对象
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告" 
                                                                                     message:@"真的要关闭 !" 
                                                                              preferredStyle:UIAlertControllerStyleAlert];
    
            // 显示,模态视图显示
            [self presentViewController:alertController animated:YES completion:nil];
    
        // 2. 创建时添加按钮等信息
    
            // 实例化 UIAlertController 对象
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告" 
                                                                                     message:@"真的要关闭 !" 
                                                                              preferredStyle:UIAlertControllerStyleAlert];
    
            // 创建按钮
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" 
                                                                   style:UIAlertActionStyleCancel 
                                                                 handler:nil];
    
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" 
                                                               style:UIAlertActionStyleDefault 
                                                             handler:nil];
            UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"考虑一下" 
                                                               style:UIAlertActionStyleDestructive 
                                                             handler:nil];
            // 向 alertController 上添加按钮
            [alertController addAction:cancelAction];
            [alertController addAction:okAction];
            [alertController addAction:noAction];
    
            // 显示 alertController 视图
            [self presentViewController:alertController animated:YES completion:nil];
  • Swift

        // 1. 创建时不添加按钮
    
            // 实例化 alertController 对象
            let alertController:UIAlertController = UIAlertController(title: "警告", 
                                                                    message: "真的要关闭 !", 
                                                             preferredStyle: .Alert)
    
            // 显示,模态视图显示
            self.presentViewController(alertController, animated: true, completion: nil)
    
        // 2. 创建时添加按钮等信息
    
            // 实例化 alertController 对象
            let alertController:UIAlertController = UIAlertController(title: "警告", 
                                                                    message: "真的要关闭 !", 
                                                             preferredStyle: .Alert)
    
            // 创建按钮
            let cancelAction:UIAlertAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
            let okAction:UIAlertAction = UIAlertAction(title: "确定", style: .Default, handler: nil)
            let noAction:UIAlertAction = UIAlertAction(title: "考虑一下", style: .Destructive, handler: nil)
    
            // 向 alertController 上添加按钮
            alertController.addAction(cancelAction)
            alertController.addAction(okAction)
            alertController.addAction(noAction)
    
            // 显示 alertController 视图
            self.presentViewController(alertController, animated: true, completion: nil)

2、alertController 的设置

  • Objective-C

        // 设置警告框类型
        /*
            UIAlertControllerStyleActionSheet = 0,   上拉菜单,操作表,底部弹出
            UIAlertControllerStyleAlert              对话框,警告,中间弹出
        */
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告" 
                                                                                 message:@"真的要关闭 ?" 
                                                                          preferredStyle:UIAlertControllerStyleAlert];
    
        // 设置按钮类型
        /*
            UIAlertActionStyleDefault = 0,  默认蓝色按钮,可以有多个
            UIAlertActionStyleCancel,       取消按钮,显示在左侧或最下边,有且只能有一个
            UIAlertActionStyleDestructive   红色警示按钮,可以有多个,
                                            《iOS 用户界面指南》要求所有的 “警示” 样式按钮都必须排名第一
        */
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" 
                                                               style:UIAlertActionStyleCancel 
                                                             handler:nil];
    
        // 设置按钮点击响应事件
        UIAlertAction *closeAction = [UIAlertAction actionWithTitle:@"关闭" 
                                                              style:UIAlertActionStyleDestructive 
                                                            handler:^(UIAlertAction * _Nonnull action) {
    
            /*
                点击了按钮时响应的事件
            */
        }];
    
        // 添加输入框
        /*
            只能添加到 UIAlertControllerStyleAlert 上,可以添加多个
        */
        [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    
            /*
                设置添加的 textField 属性
            */
        }];
    
        // 添加点击按钮
        /* 
            添加警告框上的按钮,可以添加多个
            取消按钮在左侧或最下边,其它按钮按照添加的顺序排列
        */
        [alertController addAction:cancelAction];
    
        // 设置首选按钮
        /*
            必须在添加按钮(addAction)完成后设置,iOS 9 新添加
            首选的按钮会有加粗效果,只能用在 UIAlertControllerStyleAlert 中
        */
        alertController.preferredAction = okAction;
    
        // 设置按钮激活状态
        /*
            YES 按钮激活,可点击。NO 按钮禁用,不可点击
        */
        okAction.enabled = YES;
    
        // 显示警告框视图
        [self presentViewController:alertController animated:YES completion:nil];
    
        // 设置警告框标题
        alertController.title = @"登录";
    
        // 设置警告框提示信息
        alertController.message = @"请输入用户名和密码登录 !";
    
        // 获取警告框标题
        NSString *alertTitle = alertController.title;
    
        // 获取警告框提示信息
        NSString *alertMessage = alertController.message;
    
        // 获取警告框类型,readonly
        UIAlertControllerStyle alertStyle = alertController.preferredStyle;
    
        // 获取所有输入框,readonly
        NSArray<UITextField *> *textFieldArray = alertController.textFields;
    
        // 获取所有按钮,readonly
        NSArray<UIAlertAction *> *actionsArray = alertController.actions;
    
        // 获取按钮标题,readonly
        NSString *actionTitle = okAction.title;
    
        // 获取按钮类型,readonly
        UIAlertActionStyle actionStyle = okAction.style;
    
        // 获取首选按钮
        UIAlertAction *preferredAction = alertController.preferredAction;
    
        // 获取按钮激活状态
        BOOL actionEnabled = okAction.enabled;
  • Swift

        // 设置警告框类型
        /*
            case ActionSheet   上拉菜单,操作表,底部弹出
            case Alert         对话框,警告,中间弹出
        */
        let alertController:UIAlertController = UIAlertController(title: "警告", 
                                                                message: "真的要关闭 !", 
                                                         preferredStyle: .Alert)
    
        // 设置按钮类型
        /*
            case Default       默认蓝色按钮,可以有多个
            case Cancel        取消按钮,显示在左侧或最下边,有且只能有一个
            case Destructive   红色警示按钮,可以有多个,
                               《iOS 用户界面指南》要求所有的 “警示” 样式按钮都必须排名第一
        */
        let cancelAction:UIAlertAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
    
        // 设置按钮点击响应事件
        let closeAction:UIAlertAction = UIAlertAction(title: "关闭", 
                                                      style: .Destructive) { (action:UIAlertAction) in
    
            /*
                点击了按钮时响应的事件
            */
        }
    
        // 添加输入框
        /*
            只能添加到 UIAlertControllerStyleAlert 上,可以添加多个
        */
        alertController.addTextFieldWithConfigurationHandler { (textField:UITextField) in
    
            /*
                设置添加的 textField 属性
            */
        }
    
        // 添加点击按钮
        /*
            添加警告框上的按钮,可以添加多个
            取消按钮在左侧或最下边,其它按钮按照添加的顺序排列
        */
        alertController.addAction(cancelAction)
    
        // 设置首选按钮
        /*
            必须在添加按钮(addAction)完成后设置,iOS 9 新添加
            首选的按钮会有加粗效果,只能用在 UIAlertControllerStyleAlert 中
        */
        alertController.preferredAction = okAction
    
        // 设置按钮激活状态
        /*
            YES 按钮激活,可点击。NO 按钮禁用,不可点击
        */
        okAction.enabled = true
    
        // 显示警告框视图
        self.presentViewController(alertController, animated: true, completion: nil)
    
        // 设置警告框标题
        alertController.title = "登录"
    
        // 设置警告框提示信息
        alertController.message = "请输入用户名和密码登录 !"
    
        // 获取警告框标题
        let alertTitle:String? = alertController.title
    
        // 获取警告框提示信息
        let alertMessage:String? = alertController.message
    
        // 获取警告框类型,readonly
        let alertStyle:UIAlertControllerStyle = alertController.preferredStyle
    
        // 获取所有输入框,readonly
        let textFieldArray:[UITextField]? = alertController.textFields
    
        // 获取所有按钮,readonly
        let actionsArray:[UIAlertAction] = alertController.actions
    
        // 获取按钮标题,readonly
        let actionTitle:String? = okAction.title
    
        // 获取按钮类型,readonly
        let actionStyle:UIAlertActionStyle = okAction.style
    
        // 获取首选按钮
        let preferredAction:UIAlertAction? = alertController.preferredAction
    
        // 获取按钮激活状态
        let actionEnabled:Bool = okAction.enabled
目录
相关文章
|
iOS开发 开发者
iOS8统一的系统提示控件——UIAlertController
iOS8统一的系统提示控件——UIAlertController
142 0
iOS8统一的系统提示控件——UIAlertController
|
Android开发 iOS开发
iOS 8 引入的 UIActionSheet 和 UIAlertView 的替代品 - UIAlertController
iOS 8 引入的 UIActionSheet 的替代品 - UIAlertController 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循“署名-非商业用途-保持一致”创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。
1154 0
iOS8开发之iOS8的UIAlertController
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> 在iOS8之前用UIActionSheet和UIAlertView来提供按钮选择和提示性信息,比如UIActionSh
1282 0
|
1月前
|
iOS开发 开发者
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
155 67
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
|
3月前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台策略
在移动应用开发的战场上,安卓和iOS两大阵营各据一方。随着技术的演进,跨平台开发框架成为开发者的新宠,旨在实现一次编码、多平台部署的梦想。本文将探讨跨平台开发的优势与挑战,并分享实用的开发技巧,帮助开发者在安卓和iOS的世界中游刃有余。
|
13天前
|
JavaScript 搜索推荐 Android开发
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
40 8
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡

热门文章

最新文章

  • 1
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
  • 2
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 3
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 4
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
  • 5
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
  • 6
    iOS各个证书生成细节
  • 7
    iOS|记一名 iOS 开发新手的前两次 App 审核经历
  • 8
    解决 input 输入框在 iOS 系统中无法输入内容
  • 9
    iOS:应用程序的线程安全性
  • 10
    iOS开发-UIScrollView原理
  • 1
    iOS|记一名 iOS 开发新手的前两次 App 审核经历
    10
  • 2
    iOS各个证书生成细节
    23
  • 3
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    122
  • 4
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    40
  • 5
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    55
  • 6
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    46
  • 7
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    40
  • 8
    uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
    155
  • 9
    【05】2025年1月首发完整版-篇幅较长-苹果app如何上架到app store完整流程·不借助第三方上架工具的情况下无需花钱但需仔细学习-优雅草央千澈详解关于APP签名以及分发-们最关心的一篇来了-IOS上架app
    312
  • 10
    app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
    107