简体   繁体   English

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

[英]Access constant or variables from inside a function to in `viewdidload` to save in firebase swift 3

I am unable to access constant or variables which are defined inside a function for example i have defined a constant inside a didselectrow function now when i select a row the value of constant get printed in console but now i want to save that value in firebase so when i call that value in button it is undefined why? 我无法访问在函数内部定义的常量或变量,例如,当我选择一行时,现在我在didselectrow函数中定义了一个常量,该常量的值在控制台中打印,但现在我想将该值保存在firebase因此当我在button调用该值时,为什么不明确? what should i do that?? 我该怎么办?

here is the code i have written in button the constant countryGroup is undefined. 这是我在button编写的代码,未定义常数countryGroup because i have defined it in didselectrow 因为我已经在didselectrow定义了它

button 按键

 @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 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)")
    }

Instead of defining let country = currentCell.textLabel!.text! 代替定义let country = currentCell.textLabel!.text! in didselectrow i have defined a global variable var country = String? didselectrow我定义了一个全局变量var country = String? and set country = currentCell.textLabel!.text! 并将country = currentCell.textLabel!.text!设置country = currentCell.textLabel!.text!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM