(Swift4.0)alertController三种常用弹框
//
// AlertController.swift
// ZSwift
//
// Created by Zhanggaoju on 2018/8/31.
// Copyright © 2018年 ZhangGaoju. All rights reserved.
//
import UIKit
classAlertController:BaseViewController{
overridefuncviewDidLoad() {
super.viewDidLoad()
self.title = "alertController"
self.view.addSubview(self.alert)
self.view.addSubview(self.actionSheet)
self.view.addSubview(self.textFieldAlert)
}
lazyvaralert:UIButton= {
letalert =UIButton(frame:CGRect.init(x: (kScreenWidth-180)/2, y:100, width:180, height:30))
alert.setTitleColor(UIColor.red, for: .normal)
alert.setTitle("alert", for: .normal)
alert.addTarget(self, action:#selector(alertClick), for: .touchUpInside)
returnalert
}()
@objcfuncalertClick(){
letalertController =UIAlertController(title:"标题", message:"提示语,比如:我帅的一疋!", preferredStyle: .alert)
letokAction =UIAlertAction(title:"是的", style: .default) { (cancelAction)in
print("点击了确定")
}
letcancelAction =UIAlertAction(title:"取消", style: .cancel, handler:nil)
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.present(alertController, animated:true, completion:nil)
}
lazyvaractionSheet:UIButton= {
letactionSheet =UIButton(frame:CGRect.init(x: (kScreenWidth-180)/2, y:180, width:180, height:30))
actionSheet.setTitleColor(UIColor.blue, for: .normal)
actionSheet.setTitle("actionSheet", for: .normal)
actionSheet.addTarget(self, action:#selector(actionSheetClick), for: .touchUpInside)
returnactionSheet
}()
@objcfuncactionSheetClick(){
letalertController =UIAlertController(title:"保存或删除数据", message:"删除数据将不可恢复", preferredStyle: .alert)
letarchiveAction =UIAlertAction(title:"保存", style: .default) { (cancelAction)in
print("点击了确定")
}
letdeleteAction =UIAlertAction(title:"删除", style: .destructive, handler:nil)
letcancelAction =UIAlertAction(title:"取消", style: .cancel, handler:nil)
alertController.addAction(archiveAction)
alertController.addAction(deleteAction)
alertController.addAction(cancelAction)
self.present(alertController, animated:true, completion:nil)
}
lazyvartextFieldAlert:UIButton= {
lettextFieldAlert =UIButton(frame:CGRect.init(x: (kScreenWidth-180)/2, y:260, width:180, height:30))
textFieldAlert.setTitleColor(UIColor.purple, for: .normal)
textFieldAlert.setTitle("textFieldAlert", for: .normal)
textFieldAlert.addTarget(self, action:#selector(textFieldAlertClick), for: .touchUpInside)
returntextFieldAlert
}()
@objcfunctextFieldAlertClick(){
letalertController =UIAlertController(title:"登录", message: "请输入用户名和密码", preferredStyle: .alert)
alertController.addTextField{ (textField)in
textField.placeholder="用户名"
}
alertController.addTextField{ (textField)in
textField.placeholder="密码"
textField.isSecureTextEntry=true
}
letcanceAction =UIAlertAction(title:"取消", style: .cancel, handler:nil)
letokAction =UIAlertAction(title:"登录", style: .default) { (action)in
letlogin = alertController.textFields?.first
letpassword = alertController.textFields?.last;
print("用户名:\(String(describing: login?.text)) 密码\(String(describing: password?.text))")
}
alertController.addAction(canceAction)
alertController.addAction(okAction)
self.present(alertController, animated:true, completion:nil)
}
overridefuncdidReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}