ViewController.swift代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screen = UIScreen.main.bounds
let buttonAlertView = UIButton(type: UIButton.ButtonType.system)
buttonAlertView.setTitle("Test警告框", for: UIControl.State())
let buttonAlertViewWidth: CGFloat = 100
let buttonAlertViewHeight: CGFloat = 30
let buttonAlertViewTopView: CGFloat = 130
buttonAlertView.frame = CGRect(x: (screen.size.width - buttonAlertViewWidth)/2, y: buttonAlertViewTopView, width: buttonAlertViewWidth, height: buttonAlertViewHeight)
buttonAlertView.addTarget(self, action: #selector(testAlertView(_:)), for: .touchUpInside)
self.view.addSubview(buttonAlertView)
}
@objc func testAlertView(_ sender:AnyObject) {
let alertController: UIAlertController = UIAlertController(title: "Alert", message: "Alert text goes here", preferredStyle: UIAlertController.Style.alert)
let noAction = UIAlertAction(title: "No", style: .cancel) {
(UIAlertAction) -> Void in
print("Tap No Button")
}
let yesAction = UIAlertAction(title: "Yes", style: .default) {
(UIAlertAction) -> Void in
print("Tap Yes Button")
}
alertController.addAction(noAction)
alertController.addAction(yesAction)
self.present(alertController,animated: true, completion: nil)
}
}
image.png
网友评论