UIAlertController
创建一个警告窗口
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .system)
button.frame = CGRect(x: 20, y: 120, width: 280, height: 44)
button.setTitle("Test", for: .normal)
button.addTarget(self, action: #selector(ViewController.showAlert), for: .touchUpInside)
self.view.addSubview(button)
}
@objc func showAlert() {
let alert = UIAlertController(title: "Information", message: "Are you a student", preferredStyle: .alert)
let yes = UIAlertAction(title: "Yes", style: .default) { (UIAlertAction) in
print("Yes I am a student ")
}
let no = UIAlertAction(title: "No", style: .destructive) { (UIAlertAction) in
print("No I am not a student")
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(yes)
alert.addAction(no)
alert.addAction(cancel)
self.present(alert, animated: true, completion: nil)
}
}
创建一个动作表单
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 创建一个用于展示的按钮
let button = UIButton(type: .system)
button.frame = CGRect(x: 20, y: 120, width: 280, height: 44)
button.setTitle("Test", for: .normal)
button.addTarget(self, action: #selector(ViewController.showAlert), for: .touchUpInside)
self.view.addSubview(button)
}
@objc func showAlert() {
let actionSheet = UIAlertController(title: "Information", message: "What's your favourite?", preferredStyle: .actionSheet)
let fishing = UIAlertAction(title: "Fishing", style: .default) { (UIAlertAction) in
print("I like fishing")
}
let hunting = UIAlertAction(title: "Hunting", style: .destructive) { (UIAlertAction) in
print("I like hunting")
}
let nothing = UIAlertAction(title: "Nothing", style: .destructive) { (UIAlertAction) in
print("A life of Nonsence")
}
actionSheet.addAction(fishing)
actionSheet.addAction(hunting)
actionSheet.addAction(nothing)
self.present(actionSheet, animated: true) {
print("What's your favourite")
}
}
}
网友评论