如何使用一个UIViewController和两个UIPickerViews?

时间:2015-03-04 17:48:24

标签: ios swift uipickerview

有一些较旧的帖子,但我无法让它们工作(我尝试使用下面的标记方法)。我想在Swift中的一个UIPickerView中使用两个UIViewController,但我无法弄清楚如何描绘它们:

class ViewController: UIViewController, UIPickerViewDelegate,     UIPickerViewDataSource {



var footballWinner = [
    2015: "Patriots"
]

var hockeyWinner = [
    2015: "TBD"
]

var baseballWinner = [
    2015: "TBD"
]

var baseketballWinner = [
    2015: "TBD"
]



@IBOutlet weak var label: UILabel!


@IBOutlet weak var sportPicker: UIPickerView!

 @IBOutlet weak var yearPicker: UIPickerView!


override func viewDidLoad() {
    super.viewDidLoad()
    yearPicker.delegate = self
    yearPicker.dataSource = self
    sportPicker.delegate = self
    sportPicker.dataSource = self
}

if UIPickerView, didSelectRow == sportPIcker

class YearPickerClass {

    var years = [2015, 2013, 2012, 2011, 2010]

    var selectedYear: Int?

func numberOfComponentsInPickerView(yearPicker: UIPickerView) -> Int {
    return 1
}

func pickerView(yearPicker: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return years.count
}

func pickerView(yearPicker: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    selectedYear = years[row]
}

func pickerView(yearPicker: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return "\(years[row])"
}
}

class SportPickerClass {

      var sports = ["Football", "Baseball", "Hockey", "Basketball"]

    var selectedSport: String?

    func numberOfComponentsInPickerView(yearPicker: UIPickerView) -> Int {
        return 1
    }

    func pickerView(yearPicker: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return sports.count
    }

    func pickerView(yearPicker: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        selectedSport = sports[row]
    }

    func pickerView(yearPicker: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return "\(sports[row])"
    }

}

 }

1 个答案:

答案 0 :(得分:4)

您可以创建一个符合UIPickerViewDataSourceUIPickerViewDelegate(或只是其中一个)的任意对象,然后将delegate和/或dataSource设置为这个对象是这样的(为了简单起见,我创建了两个类,但是可以轻松创建一个带有一个带数组作为参数的便利init的类):

class ViewController: UIViewController {

    @IBOutlet weak var firstPickerView: UIPickerView!
    @IBOutlet weak var secondPickerView: UIPickerView!

    let firstPickerData = FirstPickerData()
    let secondPickerData = SecondPickerData()

    override func viewDidLoad() {
        super.viewDidLoad()

        firstPickerView.delegate = firstPickerData
        firstPickerView.dataSource = firstPickerData

        secondPickerView.delegate = secondPickerData
        secondPickerView.dataSource = secondPickerData

    }
}

class FirstPickerData: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {

    let source = ["a", "b", "c", "d", "e"]

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return source.count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return source[row]
    }
}

class SecondPickerData: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {

    let source = ["1", "2", "3", "4", "5"]

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return source.count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return source[row]
    }
}