在Swift 4.2和Xcode 10中
方法1:
简单提醒
let alert = UIAlertController(title: "Your title", message: "Your message", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: { action in
})
alert.addAction(ok)
let cancel = UIAlertAction(title: "Cancel", style: .default, handler: { action in
})
alert.addAction(cancel)
DispatchQueue.main.async(execute: {
self.present(alert, animated: true)
})
方法2:
共享类警报
如果您想使用共享类样式(每次写入一次,请在任何地方使用)
import UIKit
class SharedClass: NSObject {//This is shared class
static let sharedInstance = SharedClass()
//Show alert
func alert(view: UIViewController, title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { action in
})
alert.addAction(defaultAction)
DispatchQueue.main.async(execute: {
view.present(alert, animated: true)
})
}
private override init() {
}
}
现在在每个商品中都这样调用警报
SharedClass.SharedInstance.alert(view: self, title: "Your title here", message: "Your message here")
方法3:
当前所有Windows的警报顶部
如果要在所有视图之上显示警报,请使用此代码
func alertWindow(title: String, message: String) {
DispatchQueue.main.async(execute: {
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindowLevelAlert + 1
let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
let defaultAction2 = UIAlertAction(title: "OK", style: .default, handler: { action in
})
alert2.addAction(defaultAction2)
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alert2, animated: true, completion: nil)
})
}
函数调用
SharedClass.sharedInstance.alertWindow(title:"This your title", message:"This is your message")
方法4:
扩展警报
extension UIViewController {
func showAlert(withTitle title: String, withMessage message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: { action in
})
let cancel = UIAlertAction(title: "Cancel", style: .default, handler: { action in
})
alert.addAction(ok)
alert.addAction(cancel)
DispatchQueue.main.async(execute: {
self.present(alert, animated: true)
})
}
}
现在这样打电话
//Call showAlert function in your class
@IBAction func onClickAlert(_ sender: UIButton) {
showAlert(withTitle:"Your Title Here", withMessage: "YourCustomMessageHere")
}
方法5:
文本提示
如果要添加文本字段以发出警报。
//Global variables
var name:String?
var login:String?
//Call this function like this: alertWithTF()
//Add textfields to alert
func alertWithTF() {
let alert = UIAlertController(title: "Login", message: "Enter username&password", preferredStyle: .alert)
// Login button
let loginAction = UIAlertAction(title: "Login", style: .default, handler: { (action) -> Void in
// Get TextFields text
let usernameTxt = alert.textFields![0]
let passwordTxt = alert.textFields![1]
//Asign textfileds text to our global varibles
self.name = usernameTxt.text
self.login = passwordTxt.text
print("USERNAME: \(self.name!)\nPASSWORD: \(self.login!)")
})
// Cancel button
let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: { (action) -> Void in })
//1 textField for username
alert.addTextField { (textField: UITextField) in
textField.placeholder = "Enter username"
//If required mention keyboard type, delegates, text sixe and font etc...
//EX:
textField.keyboardType = .default
}
//2nd textField for password
alert.addTextField { (textField: UITextField) in
textField.placeholder = "Enter password"
textField.isSecureTextEntry = true
}
// Add actions
alert.addAction(loginAction)
alert.addAction(cancel)
self.present(alert, animated: true, completion: nil)
}
方法6:
带有扩展名的SharedClass中的警报
//This is your shared class
import UIKit
class SharedClass: NSObject {
static let sharedInstance = SharedClass()
//Here write your code....
private override init() {
}
}
//Alert function in shared class
extension UIViewController {
func showAlert(title: String, msg: String) {
DispatchQueue.main.async {
let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}
现在像这样直接打电话
self.showAlert(title: "Your title here...", msg: "Your message here...")
方法7:
没有共享类的警报,带有扩展的单独类中的警报。
创建一个新的Swift类,然后import UIKit
。复制并粘贴以下代码。
//This is your Swift new class file
import UIKit
import Foundation
extension UIAlertController {
class func alert(title:String, msg:String, target: UIViewController) {
let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default) {
(result: UIAlertAction) -> Void in
})
target.present(alert, animated: true, completion: nil)
}
}
现在在您所有的类中调用警报功能(单行)。
UIAlertController.alert(title:"Title", msg:"Message", target: self)
如何....
UIAlertView
并UIActionSheet
已UIAlertController
在iOS 8中被替换,您看到了吗?