将文本框值发送到UILabels

时间:2015-07-01 13:42:07

标签: ios swift uilabel

parent::parent

基本上我正在做的是这样的:当我在文本框中键入一定数字(让我们说70)时,标签会显示某些内容(例如,您的数字是70)。老实说,我不知道我在做什么。请帮忙。

2 个答案:

答案 0 :(得分:1)

你需要4件事

  • 为文字视图添加插座,以便您@IBOutlet weak var textView: UITExtView!

  • ViewController实施UITextViewDelegate

ViewController: UIViewController, UITextViewDelegate
  • self textView viewDidLoad的{​​{1}}代表。{/ li>
textView.delegate = self
  • 实施textViewDidChange并将您的逻辑放在那里
func textViewDidChange(textView: UITextView) { 
    // change your label text
}

这里有一个完整的课程示例

class ViewController: UIViewController, UITextViewDelegate  {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.textView.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func textViewDidChange(textView: UITextView) { 
        displayLabel.text = "your text"
    }

    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var typingSpace: UIView!
    @IBOutlet weak var displayLabel: UILabel!
}

答案 1 :(得分:0)

注意: @Claudio Redi 提供了正确的答案,这是从UITextField填充标签的另一种方法。

这是完成任务的一种方法:

class ViewController: UIViewController {

    @IBOutlet weak var typingSpace: UITextField!

    @IBOutlet weak var displayLabel: UILabel!

    @IBAction func handleButtonPressed(sender: UIButton) {
        displayLabel.text = typingSpace.text
    }

    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.
    }


}