从函数内部访问常量或变量到`viewdidload`以保存在firebase swift 3中

时间:2017-04-17 12:02:57

标签: ios swift xcode firebase firebase-realtime-database

我无法访问函数内部定义的常量或变量,例如我已经在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)")
    }

1 个答案:

答案 0 :(得分:0)

我没有在let country = currentCell.textLabel!.text!中定义didselectrow,而是定义了一个全局变量var country = String?并设置了country = currentCell.textLabel!.text!

相关问题