iOS-Swift 开发的各种坑

Swift - UIAlertController

2019-01-06  本文已影响0人  iOS分享

添加AlertController

import UIKit
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", message: "sub title", 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
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, animated: true, completion: nil)
        //移除提示框
        self.presentedViewController?.dismiss(animated: false, completion: nil)
    }
}

自动消失的提示框

UIAlertExtension:

import UIKit
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, animated: true, completion: nil)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
            self.presentedViewController?.dismiss(animated: true, completion: nil)
    }
  }
}

封装

创建一个swift文件,命名为UIAlertExtension

import UIKit
//对UIAlertController进行扩展
extension UIAlertController{
    //创建样式
    static func showAlert(message:String,in viewController:UIViewController){
        let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: nil))
        viewController.present(alert, animated: true, completion: nil)
    }
    //指定样式播放视图
    static func showAlert(message: String) {
        if let vc = UIApplication.shared.keyWindow?.rootViewController {
            showAlert(message: message, in: vc)
        }
    }
    //创建样式
    static func ShowConfirm(messgae:String,in viewController:UIViewController,confirm:((UIAlertAction)->Void)?){
        let alert = UIAlertController(title: nil, message: messgae, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "取消", style: .default, handler: nil))
        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: confirm))
        viewController.present(alert, animated: true, completion: nil)
    }
    //指定播放样式的视图和确定按钮的动作
    static func showConfirm(message:String,confirm:((UIAlertAction)->Void)?){
        if let vc = UIApplication.shared.keyWindow?.rootViewController{
            ShowConfirm(messgae: message, in: vc, confirm: confirm)
        }
    }
}

ViewController.swift:

import UIKit

class ViewController: UIViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //使用扩展
        UIAlertController.showAlert(message: "保存成功")
        UIAlertController.showConfirm(message: "是否确认") { (UIAlertAction) in
            print("确认成功")
    }
  }
}
上一篇下一篇

猜你喜欢

热点阅读