Swift 提示框 UIAlertViewController

//  提示框有 UIAlertView(iOS8前) 和 UIAlertController(iOS8起)

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        /// alertView 不能直接放在viewDidLoad()方法中,放在里面会爆 “Attempt to present <UIAlertController: 0x7f8e59d65550> on <Swift_UIAlertView.ViewController: 0x7f8e59f1a4d0> whose view is not in the window hierarchy!” 的错误,这是因为视图还没加载出来,就用presentView,而出现的

        let button = UIButton(type:UIButtonType.Custom)
        button.frame = CGRectMake(0, 20, 60, 60)
        button.backgroundColor = UIColor.blackColor()
        button.setTitle("弹框", forState: .Normal)
        button.addTarget(self, action: #selector(buttonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button)

        let button1 = UIButton(type:UIButtonType.Custom)
        button1.frame = CGRectMake(0, 100, 60, 60)
        button1.backgroundColor = UIColor.blackColor()
        button1.setTitle("弹框1", forState: .Normal)
        button1.addTarget(self, action: #selector(buttonAction1(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button1)
    }

//

    func buttonAction(sender:UIButton) {

        /**
         1,下面代码创建并弹出一个告警框,并带有“取消”“确定”两个按钮
         UIAlertControllerStyle:
             ActionSheet 从屏幕底部弹出并显示,点击alertView 外部 隐藏提示框
             Alert 在屏幕中部显示,点击alertView 外部无效果

        */
        let alertView = UIAlertController(title: "系统提示",message: "我是美女吗?", preferredStyle: UIAlertControllerStyle.ActionSheet)
        /**
         2,UIAlertAction:
            cancel 那么字体会变粗;
            destructive,字体会显示红色。
         */

        // 传参
        let alertAction1 = UIAlertAction(title: "是的", style: UIAlertActionStyle.Default, handler:{(alertAction:UIAlertAction)->()in

            print("我是大美女")
        })

        let alertAction2 = UIAlertAction(title: "beautiful girl!", style: UIAlertActionStyle.Destructive, handler: {
            (alerts: UIAlertAction!)->Void in
            print("倾国倾城")
        })


        // 不传参的情况下
        let alertAction3 = UIAlertAction(title: "的确是", style: UIAlertActionStyle.Cancel, handler: { _ in
            print("取消")
        })
        // action 添加到alertView 中
        alertView.addAction(alertAction1)
        alertView.addAction(alertAction2)
        alertView.addAction(alertAction3)

        self.presentViewController(alertView, animated: true, completion: nil)
    }

    func buttonAction1 (button:UIButton){
        let alertView = UIAlertController(title: "", message: "一起去旅游?", preferredStyle: UIAlertControllerStyle.Alert)
        let alertAction1 = UIAlertAction(title: "好的",style: .Destructive, handler: {
            action in switch action.style{

            case .Default:
                print("OK")
            case .Cancel:
                print("cancel")
            case .Destructive:
                print("Destructive")

                // 取出文本内容
                let textfield:UITextField = (alertView.textFields?.first)!

                print(textfield.text)
            }

        })

        let alertAction2 = UIAlertAction(title: "取消", style: .Cancel, handler: {_ in
            print("cancel")
        })
        // 添加输入框
        alertView.addTextFieldWithConfigurationHandler { (textField :UITextField) in
            textField.placeholder = "输入地名"
            textField.secureTextEntry = true
        }

        alertView.addAction(alertAction1)
        alertView.addAction(alertAction2)

        self.presentViewController(alertView, animated: true, completion: nil);

    }

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


}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值