我的UIPickerView出现问题。我的代码目前接受选择器选择的行并将其存储在Firebase中。但是,我注意到Firebase中记录的值与我的应用模拟器中的选择器视图中选择的值不同。更具体地说,该值将始终是高于或低于我在拾取器视图中预期的几行。我的代码如下:
import UIKit
import Firebase
import FirebaseAuth
class DormViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var dormName: UIPickerView!
@IBOutlet weak var entryway: UIPickerView!
@IBOutlet weak var roomNumber: UIPickerView!
@IBOutlet weak var nextButton: UIButton!
var ref: DatabaseReference!
var dorm = ""
var entry = ""
var num = ""
let dorms = ["Adams", "Apley", "Cabot", "Canaday", "Currier", "DeWolfe 10", "Dewolfe 20", "Dunster", "Eliot", "Fairfax", "Grays", "Greenough", "Hollis", "Holworthy", "Hurlbut", "Inn at Harvard", "Kirkland", "Leverett Towers", "Leverett McKinlock Hall", "Lionel", "Lowell", "Massachusetts Hall", "Mather Lowrise", "Mather Tower", "Matthews", "Mower", "New Quincy", "Pennypacker", "Pforzheimer", "Pforzheimer Jordan North", "Pforzheimer Jordan South", "Prescott", "Stone Hall", "Stoughton", "Straus", "Thayer", "Weld", "Wigglesworth", "Winthrop" ]
let entrances = ["N/A", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
let rooms = ["N/A", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99"]
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
dormName.tag = 0
entryway.tag = 1
roomNumber.tag = 2
// Do any additional setup after loading the view.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 0 {
dorm = dorms[row]
return dorm
}
else if pickerView.tag == 1 {
entry = entrances[row]
return entry
}
else {
num = rooms[row]
return num
}
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 0 {
return dorms.count
}
else if pickerView.tag == 1 {
return entrances.count
}
else {
return rooms.count
}
}
@IBAction func nextButton(_ sender: Any) {
let user = Auth.auth().currentUser
if user != nil {
self.ref.child("Users").child((user?.uid)!).updateChildValues(["Location": dorm + " " + entry + "-" + num])
performSegue(withIdentifier: "tohomepage", sender: self)
}
else {
print("error")
}
}
}
例如,如果我打算选择Adams A-3,Firebase可能会将我的选择注册为Cabot D-7。这是我的故事板的屏幕截图:
[![截图] [1] [1]
我的Firebase截图:
请让我知道为什么我的选择器视图没有注册我选择的值。谢谢!
答案 0 :(得分:1)
我认为您错过了存储选定值的选择器委托方法。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 0 {
dorm = dorms[row]
}
else if pickerView.tag == 1 {
entry = entrances[row]
}
else {
num = rooms[row]
}
}