如何通过 UIButton 将我的 UITextfield 发送到变量?

How to send my UITextfield to a variable via an UIButton?

我想知道如何在 UITextfield 中键入内容,然后通过 UIButton 将其发送到变量?

当我尝试使用此代码时,应用程序会自动崩溃并显示此错误:

Thread:1 signal SIGABRT

代码如下:

  import UIKit
  import Foundation

  class ViewController: UIViewController {

@IBOutlet weak var LabelHeure: UILabel!

@IBOutlet weak var Heure: UITextField!

@IBAction func Send(sender: AnyObject) {
     var one = String(Heure.text)
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    LabelHeure.text = NSDateFormatter.localizedStringFromDate(NSDate(),dateStyle:NSDateFormatterStyle.MediumStyle,timeStyle: NSDateFormatterStyle.NoStyle)

}

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

}

}

您不需要将 Heure.text 转换为字符串,因为它已经是一个字符串。

这是我使用的代码并且有效。按下按钮后,变量会更新为文本字段中的文本。我用了一个println来验证。

import UIKit
import Foundation

class ViewController: UIViewController {

@IBOutlet weak var LabelHeure: UILabel!

@IBOutlet weak var Heure: UITextField!

@IBAction func Send(sender: AnyObject) {

    var myVariable = Heure.text
    println(myVariable)
}


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.

}

}