Xcode Swift 3 Picker:将数据源放入 ViewDidLoad

Xcode Swift 3 Picker: Putting the DataSource in ViewDidLoad

我正在尝试设置一个根据特定条件显示不同结果的选择器。唯一的问题是我需要将 dataSource(将显示的选择器数据)放在 viewDidLoad 之外。如果满足条件,我需要 viewDidLoad 加载适当的数据。但是,对于我所见过的处理选择器的代码,dataSource 数组似乎位于 viewDidLoad 之外(我想委托方法可以使用该值)。我只是想知道我是否可以将这个 dataSource 放入 viewDidLoad 以便它能够识别它,或者是否有办法 运行 在 [=14= 之外的 Switch 或 If 语句] 和 class.

import UIKit
import Parse

class VC: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    var pickerData = [String]()

    @IBOutlet weak var namePicker: UIPickerView!

    override func viewDidLoad() {
        super.viewDidLoad()

        namePicker.dataSource = self
        namePicker.delegate = self


        //here I want the picker to show something different depending on the value of nameOfPerson. I changed the order of the array because the picker selects the first name of the array as the default. 
        switch nameOfPerson
        {
        case "Carly": 
            let pickerData = ["Carly","Jeff","Bob","Anita"]
        case "Jeff": 
            let pickerData = ["Jeff","Carly","Bob","Anita"]
        case "Bob": 
            let pickerData = ["Bob","Carly","Jeff","Anita"]
        case "Anita": 
            let pickerData = ["Anita","Carly","Jeff","Bob"]
        }
    }
   func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData.count
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerData[row]
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {

    }
}

需要在加载视图时设置数据源,但您可以在任何需要的地方加载它。

里面viewDidLoad

picker.datasource = self

任何地方都可以设置实际数据;

picker.dataArray = "example, anything"

我不确定我是否完全理解您的意思。如果您不需要这个,请告诉我。

I am just wondering if I can place this dataSource into viewDidLoad so that it would recognize it, or if there was a way to run a Switch or If statement outside the viewDidLoad and in the class

没有。数据源方法必须是 methods,即 class 顶层的函数,以便 Objective-C 可以看到它们。因此,数据本身必须对数据源方法可见,并且在 viewDidLoad 和数据源方法之间共享数据的唯一连贯方式是 属性,就像您的 var pickerData

你的代码有什么问题是 let:

    case "Carly": 
        let pickerData = ["Carly","Jeff","Bob","Anita"]
    case "Jeff": 
        let pickerData = ["Jeff","Carly","Bob","Anita"]
    case "Bob": 
        let pickerData = ["Bob","Carly","Jeff","Anita"]
    case "Anita": 
        let pickerData = ["Anita","Carly","Jeff","Bob"]
    }

从每个案例中删除术语 let!否则你只是在设置一个临时局部变量 pickerData,这不是你想要做的。您想要设置您声明的 属性

var pickerData = [String]()

...而您的代码没有这样做!你要的是这个:

case "Carly": 
    self.pickerData = ["Carly","Jeff","Bob","Anita"]
case "Jeff": 
    self.pickerData = ["Jeff","Carly","Bob","Anita"]
case "Bob": 
    self.pickerData = ["Bob","Carly","Jeff","Anita"]
case "Anita": 
    self.pickerData = ["Anita","Carly","Jeff","Bob"]
}

(当然,你还需要 default 箱子。)

然后,最后为了保险起见,重新加载数据:

self.namePicker.reloadAllComponents()