我正在做一个警报,有几个文本字段,这将保存在服务器上,一旦保存命中。我设法让它发挥作用,但发现一些我不明白的奇怪东西。我不确定如何用文字解释它,所以我会发布有问题的代码。现在UIAlertController addTextFieldWithConfigurationHandler。奇怪的功能
var enteredName: UITextField!
let myAlert: UIAlertController = UIAlertController(title: "", message: nil, preferredStyle: .Alert)
let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in
println(self.enteredName.text)
}
myAlert.addAction(saveAction)
myAlert.addTextFieldWithConfigurationHandler { textField -> Void in
textField.placeholder = "Name"
self.enteredName = textField
}
self.presentViewController(myAlert, animated: true, completion: nil)
,上面的代码工作,我也没有与它的问题,但是,我有一个非常相似的代码块原本没有工作。上面的代码片段将显示一个带有文本字段的提醒,输入内容并点击保存后会打印您输入的内容。下面的代码应该这样做,但打印空行,我假设空字符串。
var enteredName: String!
let myAlert: UIAlertController = UIAlertController(title: "", message: nil, preferredStyle: .Alert)
let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in
println(self.enteredName)
}
myAlert.addAction(saveAction)
myAlert.addTextFieldWithConfigurationHandler { textField -> Void in
textField.placeholder = "Name"
self.enteredName = textField.text
}
self.presentViewController(myAlert, animated: true, completion: nil)
正如你所看到的,没有太多改变,对我来说,两个代码块看起来应该运行。差异:在代码片段没有工作
- 变量是一个字符串,而不是为UITextField
- 变量被保存到的TextField.text而不是文本框
- 警报被打印的自我。而不是self.enteredName.text
所以我的问题是这样的,为什么底部的文本块不能正常工作而顶部块呢?
虽然我知道这不是必需的,但我会很感激你为什么投票的意见,所以我可以编辑它以更好地符合,或理解我做错了什么,所以我的下一个问题将更多关于主题。 – TaylorAllred 2015-04-02 22:23:32