文本输入框内容改变时响应,并获取最新内容

2017-03-22  本文已影响0人  焉逢12
import UIKit

class FirstViewController: UIViewController {

    @IBOutlet weak var text: UITextField!
    
    @IBOutlet weak var btn: UIButton!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        btn.addTarget(self, action: #selector(btnClick(_ :)), for: .touchUpInside)
        
        //响应输入框的编辑事件(allEditingEvents)就可以获取最新的内容。
        text.addTarget(self, action: #selector(textchange(_ :)), for: .allEditingEvents)
    }
    func btnClick(_ btn:UIButton)  {
        print("111111")
    }
    func textchange(_ text:UITextField)  {
        self.btn.isEnabled = (text.text?.characters.count)!>0
    }

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

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
上一篇下一篇

猜你喜欢

热点阅读