import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .custom)
button.frame = CGRect(x: 100.0, y: 100.0, width: 100.0, height: 50.0)
button.setTitle("按钮", for: .normal)
button.backgroundColor = UIColor.red
button.addTarget(self, action: #selector(buttonClickAction(button:)), for: .touchUpInside)
self.view.addSubview(button)
}
@objc func buttonClickAction(button:UIButton) -> Void {
let alertVC = UIAlertController(title: "提示", message: "这是一个警告框", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (cancelAction:UIAlertAction) -> Void in
NSLog("取消")
}
let sureAction = UIAlertAction(title: "确定", style: .default) { (UIAlertAction) -> Void in
NSLog("确定")
}
alertVC.addAction(cancelAction)
alertVC.addAction(sureAction)
self.present(alertVC, animated: true, completion:nil)
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .custom)
button.frame = CGRect(x: 100.0, y: 100.0, width: 100.0, height: 50.0)
button.setTitle("按钮", for: .normal)
button.backgroundColor = UIColor.red
button.addTarget(self, action: #selector(buttonClickAction(button:)), for: .touchUpInside)
self.view.addSubview(button)
}
@objc func buttonClickAction(button:UIButton) -> Void {
let alertVC = UIAlertController(title: "提示", message: "这是一个警告框", preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (cancelAction:UIAlertAction) -> Void in
NSLog("取消")
}
let sureAction = UIAlertAction(title: "确定", style: .default) { (UIAlertAction) -> Void in
NSLog("确定")
}
let questionAction = UIAlertAction(title: "问题", style: .default) { (UIAlertAction) -> Void in
NSLog("问题")
}
alertVC.addAction(questionAction)
alertVC.addAction(sureAction)
alertVC.addAction(cancelAction)
self.present(alertVC, animated: true, completion:nil)
}
}
网友评论