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

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.
    }
    */

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容