iOS学习笔记(十二):UIAlertController

在iOS 8系统之前,UIKit中有两个独立的视图控件用于在界面上弹出一个警告视图,分别是UIAlertView和UIActionSheet。iOS 8之后,苹果将UIAlertView和UIActionSheet进行了规范与统一,将它们合并成新的视图控制器——UIAlertController。

一、UIAlertController的警告框

在应用中我们经常会使用警告框对用户的某些敏感操作提出警告。例如,当用户单击某个删除动作的按键时,一般会弹出一个警告框,警告用户是否确认删除。当用户单击确认后,才真正执行删除。下面我们在ViewController类中实现一个当用户单击屏幕时会触发的方法来演示,示例代码如下:

import UIKit

class ViewController: UIViewController {
    //touchesBegan方法在用户单击屏幕时调用
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        //定义警告对话框与按钮
        let alertView = UIAlertController(title: "标题", message: "提示信息", preferredStyle: .alert)
        let action1 = UIAlertAction(title: "好", style: .default, handler: { (UIAlertAction) -> Void in
            print("好")
        })
        let action2 = UIAlertAction(title: "取消", style: .cancel, handler: { (UIAlertAction) -> Void in
            print("取消")
        })
        let action3 = UIAlertAction(title: "注意", style: .destructive, handler: { (UIAlertAction) -> Void in
            print("注意")
        })
        alertView.addAction(action1)
        alertView.addAction(action2)
        alertView.addAction(action3)
        alertView.addTextField { (textField) in
            textField.placeholder = "place"
        }
        //UIAlertController也是一种视图控制器,所以要present跳转
        self.present(alertView, animated: true, completion: nil)
    }
    
}

运行工程,单击屏幕后,效果如图所示:

touchesBegin方法是在用户单击屏幕时被系统自动调用,在这个方法中我们实现了对警告视图的创建和设置。

在创建UIAlertController对象的过程中,构造函数的第一个参数为警告视图的标题;第二个参数为警告视图的内容;第三个参数为警告视图的风格,有两种风格可选,分别是UIAlertControllerStyleAlert(警告框风格)与UIAlertControllerStyleActionSheet(活动列表风格)。

UIAlertAlertAction可以理解为是一个封装了触发方法的按钮,其构造函数的第一个参数为按钮标题;第二个参数为按钮风格,有三种风格可以选择,分别是UIAlertActionStyleDefault(默认风格)、UIAlertActionStyleCancel(取消风格)、UIAlertActionStyleDestructive(消极风格);第三个参数为点按按钮后要执行的方法。

UIAlertController的addAction方法将警告视图内添加一个选项按钮。addTextField方法向警告框中添加一个输入框,开发者可以在代码块中对输入框TextField进行设置。只有当UIAlertController风格为UIAlertControllerStyleAlert时,才能使用addTextField进行输入框添加,否则会造成错误。

要特别注意的是,因为UIAlertController是一种视图控制器,所以要使用self.present进行跳转。

对UIAlertControllerStyleAlert风格而言,当选项按钮不超过两个时,按钮会横向并列排列,超过两个则会纵向排列。

二、UIAlertController的活动列表

活动列表的作用于警告框类似,只是UI展现形式不同。示例代码如下:

import UIKit

class ViewController: UIViewController {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        let alertView = UIAlertController(title: "标题", message: "提示信息", preferredStyle: .actionSheet)
        let action1 = UIAlertAction(title: "好", style: .default, handler: { (UIAlertAction) -> Void in
            print("好")
        })
        let action2 = UIAlertAction(title: "取消", style: .cancel, handler: { (UIAlertAction) -> Void in
            print("取消")
        })
        let action3 = UIAlertAction(title: "注意", style: .destructive, handler: { (UIAlertAction) -> Void in
            print("注意")
        })
        alertView.addAction(action1)
        alertView.addAction(action2)
        alertView.addAction(action3)
        self.present(alertView, animated: true, completion: nil)
    }
    
}

运行工程,效果如下所示:

《iOS学习笔记(十二):UIAlertController》上有1条评论

发表回复

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