我无法访问函数内部定义的常量或变量,例如我已经在didselectrow
函数中定义了一个常量,现在当我选择一行时,常量的值在控制台中打印但现在我想要要在firebase
中保存该值,那么当我在button
中调用该值时,为什么不确定?我该怎么办?
这是我在button
中编写的代码,常量countryGroup
未定义。因为我已经在didselectrow
按钮
@IBAction func createAccountAction(_ sender: Any) {
ref.child("user_registration").child(UserID!).setValue(["username": fullName.text, "email": emailTextField.text, "contact": numberText.text, "city": myCity.text, "state": countryText.text, "country": country])
}
didselectrow
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == tableViewB {
let selectedData = dropDownList[indexPath.row]
buttonB.setTitle(selectedData, for: .normal)
self.tableViewB.isHidden = true
self.flag = 1
let indexPath = tableViewB.indexPathForSelectedRow
let currentCell = tableViewB.cellForRow(at: indexPath!)! as UITableViewCell
let country = currentCell.textLabel!.text!
print("\(country)")
}
答案 0 :(得分:0)
我没有在let country = currentCell.textLabel!.text!
中定义didselectrow
,而是定义了一个全局变量var country = String?
并设置了country = currentCell.textLabel!.text!