我试图加载一个基于firebase字符串的向量选择器。
这是我的内裤:

var modeOfLearning = [String] ()
    let modeOfLearning1 = ["Select Mode:",
                          "In Person",
                          "Web-based",
                          "Simulation"]
    let modeOfLearning2 = ["Select Mode:",
                          "In Person",
                          "Web-based"]

在viewDidLoad中,我从firebase获取字符串的值,并使用modeOfLearning1或2加载var modeOfLearning:
 override func viewDidLoad() {
        super.viewDidLoad()

let userEmail : String = (Auth.auth().currentUser?.email)!
        Database.database().reference().child("Student Profiles").queryOrdered(byChild: "email").queryEqual(toValue: userEmail).observe(.childAdded) { (snapshot) in

            let snapshotValue = snapshot.value as! Dictionary<String,String>

            let ustYear = snapshotValue["ustyear"]
            if ustYear == "First Year" {
                self.modeOfLearning = self.modeOfLearning1
} else if ustYear == "Second Year" {
                self.modeOfLearning = self.modeOfLearning2
}}

这是pickerView函数:
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.modeOfLearning.count}

在pickerView函数中,modeOfLearning的计数始终为零。这告诉我pickerView函数在viewDidLoad完成之前正在运行。我该怎么做才能让pickerView函数等到var modeOfLearning被加载?
任何帮助都将不胜感激!提前谢谢!

最佳答案

而不是这样做self.modeOfLearning = self.modeOfLearning1
尝试self.modeOfLearning.append(self.modeOfLearning),并在完成处理程序调用pickerview.reloadAllComponents()内,以便触发选择器视图函数。

10-08 14:00