美文网首页
UIAlertController

UIAlertController

作者: CaptainRoy | 来源:发表于2019-09-25 17:13 被阅读0次
  • alert
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)
        
    }
    
}
  • actionSheet
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)
        
    }
    
}

相关文章

网友评论

      本文标题:UIAlertController

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