【问题标题】:How to send my UITextfield to a variable via an UIButton?如何通过 UIButton 将我的 UITextfield 发送到变量?
【发布时间】:2015-08-12 22:10:32
【问题描述】:

我想知道如何在 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.

}

}

【问题讨论】:

  • 您想为在 Heure 中输入的文本设置什么? LabelHeure?
  • 您的Heure 插座是否真的连接到界面生成器中的文本字段控件?在编辑器中它旁边应该有一个圆圈。空表示未连接,填充表示已连接。如果未连接,则会发生类似错误。

标签: iphone swift uibutton uitextfield uilabel


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    }

    【讨论】:

    • 谢谢,但是还是有SIGABRT错误我验证了所有的连接...还是不明白...
    • 尝试重新启动 Xcode,然后清理项目。有时会发生这种情况。
    • de rien :) 如果您再次需要帮助,请在此处评论,我会再看一看!
    • 好的! C'est très gentil ;) !
    • 我只是想知道是否有一种方法可以每天将我的 UITextfield 内容注册到不同的变量中?
    猜你喜欢
    • 2021-02-20
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 2017-03-14
    • 1970-01-01
    相关资源
    最近更新 更多