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

标签 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)")
    }

最佳答案

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

关于ios - 从函数内部访问常量或变量到 `viewdidload`以保存在firebase swift 3中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43451107/

相关文章:

ios - UILabel 的图层在动画时不更新颜色

ios - 为什么 Xcode 会为 IBOutlet 创建弱引用?

swift - 在 AppDelegate 中预加载 adMob 插屏广告并在其他 UIViewController 中显示广告

Xcode 8 文件缩进 : How to set 2 spaces as default configuration?

ios - 如何在 Swift 中以编程方式在 UILabel 中插入自定义表情符号?

ios - 混合 UIScrollViews 和 UITableViews

ios - 将缩小 View 的图像与 subview 中保持原始大小的图像合并

Swift singlelinkedlist节点不切断列表

swift - 如何在 swift 中在两个以上 View 之间切换

php - 使用 PHP 从 iOS 应用程序向数据库提交数据时出错