Swift基础(二十二)UIAlertView,UIActionSheet,UIAlertController

本文详细介绍了如何使用 UIAlertController 创建带有不同样式的警告对话框和操作表,包括如何添加按钮、文本框及设置代理处理用户交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明:使用UIAlertController时,在显示时,需要“模态”出这个空件,最好是自定义一个类,否则很可能会“模态”不出来

class RootViewController: UIViewController, UIAlertViewDelegate, UIActionSheetDelegate {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        self.view.backgroundColor = UIColor.whiteColor()
        
        // UIAlertView
        // 创建单一按钮提醒视图
        let oneButtonAlert: UIAlertView = UIAlertView(title: "提醒", message: "这是一个简单的提醒视图", delegate: nil, cancelButtonTitle: "确定")
        // 显示提醒视图
//        oneButtonAlert.show()
        
        // 创建多按钮提醒视图
        let moreButtonAlert: UIAlertView = UIAlertView(title: "提醒", message: "多按钮提醒钮视图,请选择一个",delegate: nil, cancelButtonTitle: "确定", otherButtonTitles: "按钮 1", "按钮 2", "按钮 3", "按钮 4")
        // 设置代理
        moreButtonAlert.delegate = self
//        moreButtonAlert.show()
        
        // 还可以通过下面的方法创建提醒视图
        // 初始化一个空的alert
        let alert = UIAlertView()
        // 设置标题
        alert.title = "提醒"
        // 设置代理
//        alert.delegate = self
        // 设置提醒信息
        alert.message = "多按钮提醒钮视图,请选择一个按钮"
        // 添加按钮,可以添加多个
        alert.addButtonWithTitle("按钮 1")
        alert.addButtonWithTitle("按钮 2")
        alert.addButtonWithTitle("按钮 3")
        alert.addButtonWithTitle("按钮 4")
        
        // 显示
//        alert.show()
        /*
         public enum UIAlertViewStyle : Int {
         
         case Default                   默认没有文本框
         case SecureTextInput           一个密码输入框
         case PlainTextInput            一个明文文本框
         case LoginAndPasswordInput     具有账号输入框和密码输入框
         }
         */
        // 设置提醒视图样式
        oneButtonAlert.alertViewStyle = .LoginAndPasswordInput
        // 可以获取到提醒视图上的文本框
//        let textField: UITextField? = oneButtonAlert.textFieldAtIndex(0)
        
        // 设置文本框占位符
//        textField?.placeholder = "请输入账号"
        
        
        // UIActionSheet
        // 操作表经常有用户分享功能的实现,展现多个分享按钮共用户选择
        // 创建不带红色按钮的操作表代码
        let actionSheet: UIActionSheet = UIActionSheet(title: "请选择分享方向", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "邮件", "短信", "微博", "微信")
        // 显示到视图上
//        actionSheet.showInView(self.view)
        // 创建带有红色按钮的操作表
        var actionSheet2: UIActionSheet = UIActionSheet(title: "将删除该条评论", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: "确定删除")
        // 显示到视图上
//        actionSheet2.showInView(self.view)
        
        // 常用属性
        // 设置标题
        actionSheet.title = "新标题"
        // 设置样式
        actionSheet.actionSheetStyle = UIActionSheetStyle.BlackTranslucent
        // 添加按钮
        actionSheet.addButtonWithTitle("新加按钮")
        // 根据index坐标获取一个按钮的文本
        let butIndex = actionSheet.buttonTitleAtIndex(2)
        // 获取取消按钮的坐标
        let cancelIndex = actionSheet.cancelButtonIndex
        // 获取按钮个数
        let butCount = actionSheet.numberOfButtons
        
        // 苹果官方现在不提倡在iOS 8 中使用UIAlertView和UIActionSheet,取而代之的是UIAlertController。
        // 在iOS 8 中,UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两项的功能和作用。使用对话框(alert)还是使用上拉菜单(actionsheet),取决于在创建控制器时是如何设置首选样式的
        // 创建样式是Alert的UIAlertController
        let alertController = UIAlertController(title: "提示", message: "确定退出登录?", preferredStyle: UIAlertControllerStyle.Alert)
        // 创建取消按钮
        let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: { (action: UIAlertAction) -> Void in
            print("取消")
        })
        // 创建确定按钮
        let otherAction = UIAlertAction(title: "确定", style: .Default, handler: { (action: UIAlertAction) -> Void in
            print("确定")
        })
        // 添加按钮
        alertController.addAction(cancelAction)
        alertController.addAction(otherAction)
        // 弹出
        self.presentViewController(alertController, animated: true, completion: { () -> Void in
        
        })
        
        // 带文本框的UIAlertController,一般用于登录,
        // 创建样式是Alert的UIAlertController
        // 添加文本框
        alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
            textField.placeholder = "登录"
        }
        // 添加文本框1
        alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
            textField.placeholder = "密码"
            textField.secureTextEntry = true
        }
       
        // 创建样式是ActionSheet的UIAlertController
        let alertController1 = UIAlertController(title: "提示", message: "将删除该条评论?", preferredStyle: .ActionSheet)
        // 创建取消按钮
        let cancelAction1 = UIAlertAction(title: "取消", style: .Cancel, handler: { (action: UIAlertAction) -> Void in
            print("取消")
        })
        // 创建确定按钮
        let otherAction1 = UIAlertAction(title: "确定", style: .Default, handler: { (action: UIAlertAction) -> Void in
            print("确定")
        })
        // 添加按钮
        alertController1.addAction(cancelAction1)
        alertController1.addAction(otherAction1)
        // 弹出
        self.presentViewController(alertController1, animated: true) {
            
        }
    }
    
    // 根据被单击按钮的索引处理单击事件
    func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
        switch buttonIndex {
        case 0:
            print("按钮 1")
        case 1:
            print("按钮 2")
        case 2:
            print("按钮 3")
        case 3:
            print("按钮 4")
        default:
            print("单击确定")
        }
    }
    
    // 取消按钮的事件
    func alertViewCancel(alertView: UIAlertView) {
        
    }
    
    // 即将显示时的事件
    func willPresentAlertView(alertView: UIAlertView) {
        
    }
    
    // 已经显示时的事件
    func didPresentAlertView(alertView: UIAlertView) {
        
    }
    
    // 即将消失时的事件
    func alertView(alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int) {
        
    }
    
    // 已经消失时的事件
    func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) {
        
    }
    
    // 编辑任何默认的字段添加的风格之后调用
    func alertViewShouldEnableFirstOtherButton(alertView: UIAlertView) -> Bool {
        return false
    }
    
    // 根据被单击按钮的索引处理单击事件
    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
        switch buttonIndex {
        case 0:
            print("单击取消")
        case 1:
            print("1")
        case 2:
            print("2")
        case 3:
            print("3")
        default:
            print("未知")
        }
    }
    
    // 协议方法
    // 选择取消按钮
    func actionSheetCancel(actionSheet: UIActionSheet) {
        
    }
    
    // 即将显示时
    func willPresentActionSheet(actionSheet: UIActionSheet) {
        
    }
    
    // 已显示时
    func didPresentActionSheet(actionSheet: UIActionSheet) {
        
    }
    
    // 即将消失
    func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int) {
        
    }
    
    // 已消失
    func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {
        
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值