加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift - UIAlertController

发布时间:2020-12-14 05:07:02 所属栏目:百科 来源:网络整理
导读:添加AlertController import UIKitvar imageView:UIImageView!class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let button = UIButton(frame: CGRect(x: 150,y: 250,width: 50,height: 50)) button.background

添加AlertController

import UIKit
var imageView:UIImageView!
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let button = UIButton(frame: CGRect(x: 150,y: 250,width: 50,height: 50))
        button.backgroundColor = UIColor.black
        button.addTarget(self,action: #selector(ViewController.alertView),for: .touchUpInside)
        self.view.addSubview(button)
    }
    @objc func alertView()
    {
        //创建UIAlertController(警告窗口)
        let alert = UIAlertController(title: "Information",message: "sub title",preferredStyle: .alert)
        //创建UIAlertController(动作表单)
        let alertB = UIAlertController(title: "Information",preferredStyle: .actionSheet)
        //创建UIAlertController的Action
        let OK = UIAlertAction(title: "OK",style: .default) { (UIAlertAction) in
            print("you selected ok")
        }
        let Cancel = UIAlertAction(title: "Cancel",style: .cancel) { (UIAlertAction) in
            print("you selected cancel")
        }
        //将Actiont加入到AlertController
        alert.addAction(OK)
        alert.addAction(Cancel)
        //以模态方式弹出
        self.present(alert,animated: true,completion: nil)
    }
}

添加文本输入框

import UIKit
var imageView:UIImageView!
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.presentedViewController?.dismiss(animated: false,completion: nil)
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //添加任意数量文本输入框
        let alertController = UIAlertController(title: "登陆",message: "输入账号密码",preferredStyle: .alert)
        alertController.addTextField { (textField:UITextField) in
            textField.placeholder = "用户"}
        alertController.addTextField { (textField:UITextField) in
            textField.placeholder = "密码"
            textField.isSecureTextEntry = true
        }
        let Login = UIAlertAction(title: "登陆",style: .default,handler: nil)
        let Quit = UIAlertAction(title: "退出",style: .cancel,handler: nil)
        alertController.addAction(Login)
        alertController.addAction(Quit)
        //模态弹出提示框
        self.present(alertController,completion: nil)
        //移除提示框
        self.presentedViewController?.dismiss(animated: false,completion: nil)
    }
}

自动消失的提示框

import UIKit
var imageView:UIImageView!
class ViewController: UIViewController {
    override func viewDidLoad() {
        
        }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let alert = UIAlertController(title: "自动关闭",message: "3s",preferredStyle: .alert)
        self.present(alert,completion: nil)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
            self.presentedViewController?.dismiss(animated: true,completion: nil)
    }
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读