美文网首页
swift 4.2代码实现警告框

swift 4.2代码实现警告框

作者: yytester | 来源:发表于2019-01-04 16:58 被阅读8次

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

相关文章

网友评论

      本文标题:swift 4.2代码实现警告框

      本文链接:https://www.haomeiwen.com/subject/udjkrqtx.html