我在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.
}
}
最佳答案
最好使用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.
}
}
关于swift - textField 到 LabelView live by swift 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35554885/