如何通过按钮单击变量将文本保存在文本字段中



如何在 Swift 中通过单击"保存"按钮简单地将书面文本保存到变量"a"中?

我试过这个

class YourViewController: UIViewController {
    @IBOutlet weak var textField: UITextField! //or UITextView

    //lifecycle methods and so on ...    
    @IBAction func saveTextToVar(sender: UIButton) {
        var text: NSString ?= textField.text
    }
}

但是在测试我的应用程序时,我总是收到错误。http://www.bilder-upload.eu/upload/584458-1421163318.png

您需要定义IBOutlet,然后将其与xib或情节提要连接。在您需要定义操作并将"内部触摸"按钮操作与您定义的操作连接之后。

这里的代码

class YourViewController: UIViewController {
    @IBOutlet weak var textField: UITextField! //or UITextView

    //lifecycle methods and so on ...    
    @IBAction func saveTextToVar(sender: UIButton) {
        var text: NSString ?= textField.text
    }
}

最新更新