UItextfield UItextview ❀⚘图༵文༵混༵排༵⚘❀首页投稿(暂停使用,暂停投稿)iOS进阶指南

iOS开发 - 系统弹窗输入账号密码

2016-08-19  本文已影响673人  小黑Swift
某些时候,要像类似苹果商店下载东西时候输入账号密码验证可以直接使用系统提供的弹窗控件。大家也可以根据实时检测输入的值来做进一步优化..
实时检测
    @IBAction func buttonClcik(sender: UIButton) {
        
        let alert = UIAlertController(
            title: "请输入账号密码:", message: nil, preferredStyle: .Alert)
        alert.addTextFieldWithConfigurationHandler {
            (tf:UITextField) in
            tf.placeholder = "账号"
            tf.tag = 0
            tf.addTarget(self,
                action: #selector(ViewController.textChanged(_:)), forControlEvents: .EditingChanged)
        }
        alert.addTextFieldWithConfigurationHandler {
            (tf:UITextField) in
            tf.placeholder = "密码"
            tf.keyboardType = .NumberPad
            tf.tag = 1
            tf.addTarget(self,
                action: #selector(ViewController.textChanged(_:)), forControlEvents: .EditingChanged)
        }
        func handler(act:UIAlertAction) {
            let tf = alert.textFields![0]
            let tf2 = alert.textFields![1]
            print("账号:\(tf),密码:\(tf2)")
        }
        alert.addAction(UIAlertAction(
            title: "取消", style: .Cancel, handler: nil))
        alert.addAction(UIAlertAction(
            title: "确定", style: .Default, handler: handler))
        self.presentViewController(alert, animated: true, completion: nil)
    }
    
    
    func textChanged(sender:AnyObject) {
        let tf = sender as! UITextField
        var resp : UIResponder! = tf
        while !(resp is UIAlertController) { resp = resp.nextResponder() }
        let alert = resp as! UIAlertController
        alert.actions[1].enabled = (tf.text != "")
        switch tf.tag {
        case 0:
            print("当前输入账号:\(tf.text!)")
        case 1:
            print("当前输入密码:\(tf.text!)")
        default:
            break
        }
    }
上一篇下一篇

猜你喜欢

热点阅读