我希望用户按下一个按钮,然后让他们能够在可以输入输入(为服务设置价格)的地方看到警报。另一个逻辑涉及将数据保存到数据库中,这与我的问题无关。Swift:UITextField未被识别
我用下面的例子:
https://stackoverflow.com/a/30139623/2411290
它肯定的作品,因为它正确地显示警报,但一旦我有
print("Amount: \(self.tField.text)")
“self.tField.text”不被认可。特定的错误我得到的是:类型的
值 'testVC' 没有成员 'tField'
@IBAction func setAmount(_ sender: Any) {
var tField: UITextField!
func configurationTextField(textField: UITextField!)
{
print("generating textField")
textField.placeholder = "Enter amount"
tField = textField
}
func handleCancel(alertView: UIAlertAction!)
{
print("Cancelled")
}
let alert = UIAlertController(title: "Set price of service", message: "", preferredStyle: .alert)
alert.addTextField(configurationHandler: configurationTextField)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:handleCancel))
alert.addAction(UIAlertAction(title: "Done", style: .default, handler:{ (UIAlertAction) in
print("Done !!")
}))
self.present(alert, animated: true, completion: {
print("completion block")
print("Amount: \(self.tField.text)") // Error here
})
//// other logic for app
}
我真的很感谢帮助,所以谢谢。我创建了几个全局变量,price和tField(UITextField)。我无法真正处理“完成”处理程序中的所有逻辑,因此我需要存储价格的值,因为我一次向警报外的Firebase数据库输入了5-6个值。 – user2411290
没有必要将文本字段存储为全局变量。 – rmaddy
我正在添加self.price = self.tField.text!在我的“完成”处理程序中。当我尝试将它保存到我的数据库(在警报之后)时,它似乎并不像它保存价值。我猜这不是正确的方式去做这件事。让我试试你的确切实现。 – user2411290