用swift写的两种形式的AlartView

一、创建一个button

 var btnClik:UIButton  = UIButton(type: .Custom)

    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor.redColor()

        

        btnClik.frame = CGRectMake(0, 0, 60, 54)

        btnClik.backgroundColor = UIColor.blueColor()

        btnClik.center = CGPointMake(160, 200)

        btnClik.setTitle("戳我啊", forState:.Normal)

        btnClik.addTarget(self, action:"showAlertController", forControlEvents: .TouchUpInside)

        view.addSubview(btnClik)

    }

button分别调用以下两个方法

1.原始的alertView

func showAlert(){

        let msgTitle:String = "欢迎光临"

        let message:String = "不要欢饮光临"

        let alert:UIAlertView = UIAlertView()

        alert.title = msgTitle;

        alert.message = message

        alert.addButtonWithTitle("现在去哪里")

        alert.show()

    }

 2.iOS系统9.0以后的alertView

    func showAlertController(){

        let title = "欢迎光临"

        let message = "不要欢迎光临"

        let btnLeft = "NO...."

        let btnRight = "现在带我走"

        

        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)

        let actionLeft = UIAlertAction(title:btnLeft, style: .Cancel) { action in

            

            NSLog("Not so interested in the site.")

            

        }

        

        let actionRight = UIAlertAction(title:btnRight, style: .Default) { action in

            NSLog("Hook, line and sinker!")

    }

        alertController.addAction(actionLeft)

        alertController.addAction(actionRight)

        presentViewController(alertController, animated: true, completion: nil)

    }

    

原文地址:https://www.cnblogs.com/baidaye/p/5099312.html