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

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

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.红字提示窗

红字弹窗.png

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

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

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.带有输入窗的提示窗

输入框弹窗.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)

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,229评论 4 61
  • 我接触长投的时间并不短,真正学习是在今年的5月份。之前我对事情大多是三分钟热度,发生了一件事打破了这种混吃等死的节...
    青霜台阅读 247评论 0 0
  • 前段时间儿子上课违纪了,被要求在家反省。反省的内容之一是每天一篇检査。第一天,我看了他写的的检查,大致内容如下: ...
    guocaixia阅读 440评论 5 2
  • #58号雕刻打卡Day30# 1.九点靠墙站 10分钟 感受:屁股收紧就一切都好说了,如果发现哪里没贴紧,请注意收...
    一然2016阅读 288评论 0 0