gpt4 book ai didi

swift - textField 到 LabelView live by swift 2

转载 作者:行者123 更新时间:2023-11-30 13:41:06 27 4
gpt4 key购买 nike

我在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.

}

}

最佳答案

最好使用UITextFielddelegate:

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com