我怎么能在textField中输入什么,立即在labelView中显示它而没有按钮呢?
导入UIKit
类ViewController:UIViewController {
@IBOutlet weak var labelView: UILabel!
@IBOutlet weak var textViewout: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
最好使用UITextField
' delegate
:
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var labelView: UILabel!
@IBOutlet weak var textViewout: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textViewout.delegate = self
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
labelView.text = textViewout.text
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
修改强>
对不起,它实际上并不是最好的解决方案。它会被推迟。这应该更有效地为您服务:
class ViewController: UIViewController {
@IBOutlet weak var labelView: UILabel!
@IBOutlet weak var textViewout: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textViewout.addTarget(self, action: "updateLabel", forControlEvents: .EditingChanged)
}
func updateLabel() {
labelView.text = textViewout.text
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}