[英]How can I reference the text of a UITextField in Swift?
我试图引用UITextField
的文本,以便可以在应用程序的不同位置使用它。
@IBOutlet weak var mealNameUITextField: UITextField!
@IBAction func nextButton(sender: AnyObject) {
mealNameBeingCreated = String(mealNameUITextField.text)
print(mealNameBeingCreated)
}
这给了我错误:
致命错误:解开Optional值时意外发现nil
谁能告诉我引用文本的正确方法? 另外,最好提及我要在全局变量中使用此信息。
像这样写:
@IBAction func nextButton(sender: AnyObject) {
let mealNameBeingCreated : String = mealNameUITextField.text!
print(mealNameBeingCreated)
}
@IBAction func nextButton(sender: AnyObject) {
let mealNameBeingCreated:String = self.mealNameUITextfield.text!
print(mealNameBeingCreated)
}
要么
@IBAction func nextButton(sender: AnyObject) {
let mealNameBeingCreated = self.mealNameUITextfield.text!
print(mealNameBeingCreated)
}
文本字段返回的值是可选的,我们必须使用“!”将其解包。 符号
语法取决于您正在使用的Swift语言的版本。 我正在使用支持Swift 2.1.1的XCode 7.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.