Swift学习:提示窗UIAlertController的基本使用

最近开始学习学习一下swift了 ,为了毕业后能够更有些竞争力(哈哈。。。),这里记录一下对UIAlertController这个ios的提示窗的一些基本的使用。

1.普通的提示窗


Swift学习:提示窗UIAlertController的基本使用_第1张图片
普通弹窗.png
let alert = UIAlertController(title: "弹窗", message: "这是一个弹窗", preferredStyle: .alert)
        let cancel = UIAlertAction(title: "返回", style: .cancel, handler: nil)
        let ok = UIAlertAction(title: "OK", style: .default, handler: {
            ACTION in
            print("你点击了OK")
        })
        
        alert.addAction(cancel)
        alert.addAction(ok)
        self.present(alert, animated: true, completion: nil)

2.红字提示窗

Swift学习:提示窗UIAlertController的基本使用_第2张图片
红字弹窗.png

只需要将style属性改为 .destructive 即可

let ok = UIAlertAction(title: "OK", style: .destructive, handler: nil)

3.底部上滑弹窗

Swift学习:提示窗UIAlertController的基本使用_第3张图片
底部上滑弹窗.png
let alert = UIAlertController(title: "提示", message: "是否删除", preferredStyle: .actionSheet)
        let cancel = UIAlertAction(title: "返回", style: .cancel, handler: nil)
        let del = UIAlertAction(title: "删除", style: .destructive, handler: nil)
        let ok = UIAlertAction(title: "OK", style: .default, handler: {
            ACTION in
            print("你点击了OK")
        })
        alert.addAction(del)
        alert.addAction(ok)
        alert.addAction(cancel)
        self.present(alert, animated: true, completion: nil)

4.带有输入窗的提示窗

Swift学习:提示窗UIAlertController的基本使用_第4张图片
输入框弹窗.png
        let alert = UIAlertController(title: "提示", message: "请输入用户名和密码", preferredStyle: .alert)
        
        alert.addTextField{(usernameText) ->Void in
            usernameText.placeholder = "用户名"
        }
        
        alert.addTextField{(psdText) ->Void in
            psdText.placeholder = "密码"
            psdText.isSecureTextEntry = true
        }
        
        let cancel = UIAlertAction(title: "cancel", style: .cancel, handler: nil)
        let login = UIAlertAction(title: "login", style: .default, handler: {
            ACTION in
            print(alert.textFields?.count ?? -1)
            let text1 = alert.textFields?.first?.text
            let text2 = alert.textFields?.last?.text
            print(text1!)
            print(text2!)
            self.showNormalAlert(msg: "用户名:\(text1!)\n密码:\(text2!)")
        })
        
        alert.addAction(cancel)
        alert.addAction(login)
        self.present(alert, animated: true, completion: nil)

5.移除弹窗(可用以定时移除弹窗等)

self.presentedViewController?.dismiss(animated: false, completion: nil)

嗯,基本使用大概就是这样了吧,如有问题或补充亲留言我哦 亲~

你可能感兴趣的:(Swift学习:提示窗UIAlertController的基本使用)