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.
}
*/
}
网友评论