IOS Swift 3 (firebase database) pickerView not working when loading data from database
In my code I have two pickerViews where I am fetching data from firebase where the first picker is hard-coded with data values, where based on those values I want sub-tables from firebase to be fetched and I am not able to get them right now ... Please help me please .... Thanks in Advance
Here is my code. Please let me know where I am going wrong.
@IBOutlet weak var map: MKMapView!
@IBOutlet weak var designText: UITextField!
@IBOutlet weak var nameText: UITextField!
@IBOutlet weak var designPicker: UIPickerView!
@IBOutlet weak var namePicker: UIPickerView!
@IBOutlet weak var designListButton: UIButton!
var normaletmds = [String]()
var normaltdes = [String]()
var location : Any?
var refhandle: UInt?
var refhandle1: UInt?
var key1: Any?
var values: Any?
var getKey = [String]()
var keys: [String]?
var nameString = [String]()
var adminstring = [String]()
var globalValue: String?
var nameValue: Any?
var latitudevalue : Any!
var longitudevalue : Any!
var ref : FIRDatabaseReference?
var ref1 : FIRDatabaseReference?
var latitude1: CLLocationDegrees = CLLocationDegrees()
var longitude2: CLLocationDegrees = CLLocationDegrees()
var locationManager = CLLocationManager()
override func viewDidLoad(){
ref = FIRDatabase.database().reference().child("users").child("Aishwarya")
refhandle = ref?.observe(FIRDataEventType.value, with: { (snapshot) in
let postDict = snapshot.value as? [String : AnyObject] ?? [:]
for (key, value) in postDict
{
if (key == "llattitude")
{
self.latitudevalue = value
print("latitude",self.latitudevalue!)
}
else if (key == "llongitude")
{
self.longitudevalue = value
print("logitude",self.longitudevalue!)
}
else if (key == "namee")
{
self.nameValue = value
}
}
@IBAction func designlistButton(_ sender: Any) {
if designPicker.isHidden == false{
designPicker.isHidden = true
ref = FIRDatabase.database().reference().child("users").child("users2").child(designText.text!)
refhandle = ref?.observe(FIRDataEventType.value, with: { (snapshot) in
let postDict = snapshot.value as? [String : AnyObject] ?? [:]
print("postDict=",postDict)
for (key, value) in postDict
{
self.getKey.append(key)
}
print("getvalue=", self.getKey)
self.getValue()
})
}
else
{
designPicker.isHidden = false
}
}
func getValue()
{
for get in getKey{
ref = FIRDatabase.database().reference().child("users").child("users2").child(designText.text!).child(get)
refhandle = ref?.observe(FIRDataEventType.value, with: { (snapshot) in
let postDict = snapshot.value as? [String : AnyObject] ?? [:]
print("postDict=",postDict)
for (key, value) in postDict
{
// self.getKey?.append(key)
if key == "title"
{
self.nameString.append(value as! String)
}
}
print("namestring=",self.nameString)
})
}
print("gevalue")
}
@IBAction func namePicker(_ sender: Any) {
if namePicker.isHidden == false
{
namePicker.isHidden = true
}
else
{
namePicker.isHidden = false
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
var countrows : Int = nameString.count
if pickerView == designPicker {
countrows = self.adminstring.count
}
return countrows
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
if pickerView == designPicker {
let titleRow = adminstring[row]
return titleRow
} else if pickerView == namePicker {
let titleRow = nameString[row]
return titleRow
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if pickerView == designPicker {
self.designText.text = self.adminstring[row]
} else if pickerView == namePicker {
self.nameText.text = self.nameString[row]
}
}
+3
source to share
No one has answered this question yet
Check out similar questions: