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

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

作者: 焉逢12 | 来源:发表于2017-03-22 17:27 被阅读0次
    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.
        }
        */
    
    }
    

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/opaonttx.html