• swift


    ios 8 以后苹果官方建议使用UIAlertController这个类,所以专门去网上找资料,了解了下用法,

    1、创建一个alertController

    
    
    let alertController = UIAlertController(title: "系统提示",
                message: "您确定要离开吗?", preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            let okAction = UIAlertAction(title: "好的", style: .default,
                handler: {
                    action in
                    print("点击了确定")
            })
            alertController.addAction(cancelAction)
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: {
        
            //这里可以做一些其他操作
        })

    2、创建一个actionSheet

    (注:如果上拉菜单中有“取消”按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序是如何)

    let alertController = UIAlertController(title: "保存或删除数据", message: "删除数据将不可恢复",
        preferredStyle: .actionSheet)
    let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
    let deleteAction = UIAlertAction(title: "删除", style: .destructive, handler: nil)
    let archiveAction = UIAlertAction(title: "保存", style: .default, handler: nil)
    alertController.addAction(cancelAction)
    alertController.addAction(deleteAction)
    alertController.addAction(archiveAction)
    self.present(alertController, animated: true, completion: nil)

    3、按钮使用警告模式,文字颜色变化,用来警示用户

    var okAction = UIAlertAction(title: "好的", style: .destructive, handler: nil)

    4、添加任意数量的文本输入框

     let myAlertController = UIAlertController(title:"系统登录",message:"请输入用户名和密码",preferredStyle:.alert)
            myAlertController.addTextField { (textField:UITextField) in
                textField.placeholder = "用户名"
            }
            
            myAlertController.addTextField { (textField:UITextField) in
                textField.placeholder = "密码"
                textField.isSecureTextEntry = true
            }
            
            myAlertController.addTextField { (textField:UITextField) in
                textField.placeholder = "重复密码"
                textField.isSecureTextEntry = true
            }
            
            let cancelAction = UIAlertAction(title:"取消",style:.cancel,handler:nil)
            let okAction = UIAlertAction(title:"确定",style:.default,
                   handler:{
                    action in
                    let login = myAlertController.textFields?.first
                    let passWord = myAlertController.textFields?.last
                    print("用户名是:(String(describing: login)) 密码是:(String(describing: passWord))")
            })
            
            myAlertController.addAction(cancelAction)
            myAlertController.addAction(okAction)
            self.present(myAlertController, animated: true) {
                self.view.backgroundColor = UIColor.blue
            }

    如图

     5、使用代码移除提示框

    self.dismiss(animated: true) { 
              //其他操作  
    }
  • 相关阅读:
    select选择框去掉默认的下拉箭头
    网站怎么添加ico小图标
    js实现逐字打印效果,文本逐字显示
    jQuery实现消息列表循环垂直向上滚动
    滤镜图片变黑白+图片模糊
    多选下拉框(select 下拉多选)
    JavaScript 数组相关基础方法
    h5+ IOS App中判断本地文件是否存在 plus.io.resolveLocalFileSystemURL()
    h5+ IOS App中取消视频默认全屏播放
    C# 多线程与队列操作小练刀
  • 原文地址:https://www.cnblogs.com/hero11223/p/5694853.html
Copyright © 2020-2023  润新知