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

时间:2015-08-12 22:10:32

标签: iphone swift uibutton uitextfield uilabel

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

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

  

线程:1信号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.

}

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这是我使用的代码,它起作用。按下按钮后,变量将使用文本字段中的文本进行更新。我用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.

}

}