Guest User

Untitled

a guest
Nov 14th, 2018
920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. var country = ["USA", "CANADA"]
  2.  
  3. countries
  4. Canada
  5. states
  6. Alberta
  7. British Columbia
  8. USA
  9. states
  10. Alabama
  11. Arkansas
  12.  
  13. import UIKit
  14. import FirebaseDatabase
  15.  
  16. class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
  17.  
  18. @IBOutlet weak var countryPicker: UIPickerView!
  19.  
  20.  
  21. //let country = ["USA", "Canada"] I am no longer using this hardcoded array
  22.  
  23. var countrySelected = String()
  24. var action = 0
  25.  
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28.  
  29. //this is where I reach out to the database
  30. let database = Database.database().reference()
  31. database.child("countries").observeSingleEvent(of: .value) { (Snapshot) in
  32. print(Snapshot)
  33. }
  34.  
  35. //add code here that takes the snapshot and appends the the array below
  36. var country = string()
  37.  
  38. countrySelected = country[0]
  39. countryPicker.selectRow(action, inComponent: 0, animated: false)
  40. //print(countrySelected)
  41. }
  42.  
  43. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  44. return 1
  45. }
  46.  
  47. func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  48. return country.count
  49. }
  50.  
  51.  
  52. func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  53. return country[row]
  54. }
  55.  
  56. func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  57. countrySelected = country[row]
  58. action = country.firstIndex(of: countrySelected)!
  59. //print(countrySelected)
  60. }
Add Comment
Please, Sign In to add comment