Swift - UIAlertController的简单使用

虽然语法有点变化,但是一样是在iOS8后废弃UIAlertView后才有的新方法,跟Object-C比起来方法稍有变化,但是还能看到原来的影子,效果如下:
这里写图片描述
看代码:

import UIKit

class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let alertBtn = UIButton(type: .System)
        alertBtn.frame = CGRectMake(10, 120, 300, 60)
        alertBtn.setTitle("AlertView", forState: .Normal)
        alertBtn.layer.borderWidth = 1
        alertBtn.layer.cornerRadius=5;
        alertBtn.layer.borderColor = UIColor.blackColor().CGColor
        alertBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        alertBtn.addTarget(self, action: #selector(self.AlertView), forControlEvents: .TouchUpInside)
        self.view.addSubview(alertBtn)

        let actionSheet = UIButton(type: .System)
        actionSheet.frame = CGRectMake(10, 210, 300, 60)
        actionSheet.setTitle("ActionSheet", forState: .Normal)
        actionSheet.layer.borderWidth = 1
        actionSheet.layer.cornerRadius=5;
        actionSheet.layer.borderColor = UIColor.blackColor().CGColor
        actionSheet.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        actionSheet.addTarget(self, action: #selector(self.ActionSheet), forControlEvents: .TouchUpInside)
        self.view.addSubview(actionSheet)

        let alertFieldBtn = UIButton(type: .System)
        alertFieldBtn.frame = CGRectMake(10, 300, 300, 60)
        alertFieldBtn.setTitle("AlertFieldView", forState: .Normal)
        alertFieldBtn.layer.borderWidth = 1
        alertFieldBtn.layer.cornerRadius=5;
        alertFieldBtn.layer.borderColor = UIColor.blackColor().CGColor
        alertFieldBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        alertFieldBtn.addTarget(self, action: #selector(self.AlertField), forControlEvents: .TouchUpInside)
        self.view.addSubview(alertFieldBtn)

    }
    func AlertView() {
        let alertVC = UIAlertController(title: " 警告", message: "这是一个UIAlertController做的UIAlertView", preferredStyle: .Alert)
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
            (action: UIAlertAction) -> Void in
            /**
             写取消后操作
             */
        })
        let okAction = UIAlertAction(title: "Ok", style: .Default, handler: {
            (action: UIAlertAction) -> Void in
            /**
             写确定后操作
             */
        })
        alertVC.addAction(cancelAction)
        alertVC.addAction(okAction)
        self.presentViewController(alertVC, animated: true, completion: nil)
    }
    func ActionSheet() {
        let alertVC = UIAlertController(title: " 警告", message: "这是一个UIAlertController做的UIAlertView", preferredStyle: .ActionSheet)
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
            (action: UIAlertAction) -> Void in
            /**
             写取消后操作
             */
        })
        let okAction1 = UIAlertAction(title: "Ok1", style: .Default, handler: {
            (action: UIAlertAction) -> Void in
            /**
             写Ok1后操作
             */
        })
        let okAction2 = UIAlertAction(title: "Ok2", style: .Default, handler: {
            (action: UIAlertAction) -> Void in
            /**
             写Ok1后操作
             */
        })
        alertVC.addAction(cancelAction)
        alertVC.addAction(okAction1)
        alertVC.addAction(okAction2)
        self.presentViewController(alertVC, animated: true, completion: nil)
    }
    func AlertField() {

        let alertVC = UIAlertController(title: " 登陆", message: nil, preferredStyle: .Alert)
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
            (action: UIAlertAction) -> Void in
            /**
             写取消后操作
             */
        })
        let okAction = UIAlertAction(title: "Ok", style: .Default, handler: {
            (action: UIAlertAction) -> Void in
            /**
             写确定后操作
             */
        })

        alertVC.addTextFieldWithConfigurationHandler {
            (textField: UITextField!) -> Void in
            textField.placeholder = "登录"
        }
        alertVC.addTextFieldWithConfigurationHandler {
            (textField: UITextField!) -> Void in
            textField.placeholder = "密码"
            textField.secureTextEntry = true
        }
        alertVC.addAction(cancelAction)
        alertVC.addAction(okAction)

        //要么要写在addTextFieldWithConfigurationHandler后面要么写在cancel或者ok的block里面,否则系统崩溃
        let loginField = (alertVC.textFields?.first)! as UITextField
        let passField = (alertVC.textFields?.last)! as UITextField


        self.presentViewController(alertVC, animated: true, completion: nil)


    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Demo下载地址:https://github.com/codeliu6572/Swift_UIAlertController

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingFire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值