单个 viewController 上的多个自定义选择器视图

Multiple Custom Picker Views on a single viewController

我正在尝试在同一个 viewController 上设置两个不同的 pickerView。并让它们代表不同的数据。我已经尝试了几种不同的方法来做到这一点,并在这里看到了一些与我类似的情况,但无法将它们放在一起。任何帮助都是极好的!

这里是所有相关代码:

@IBOutlet weak var txtGoalTime: UITextField!
@IBOutlet weak var goalTimePicker: UIPickerView!

@IBOutlet weak var segIntensityZones: UISegmentedControl!

@IBOutlet weak var distanceLabel: UILabel!
@IBOutlet weak var distancePicker: UIPickerView!

@IBOutlet weak var lblPaceTime: UILabel!

let pickerData = [["\(1000)", "\(500)", "\(200)", "\(100)", "\(50)"], ["\(1000)", "\(500)", "\(200)", "\(100)", "\(50)"]]
var intensityZone = 1.0
let goalTimePickerData = [["\(1)", "\(2)", "\(3)", "\(4)", "\(5)", "\(6)", "\(7)", "\(8)", "\(9)", "\(10)", "\(11)"], ["\(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)"], [".\(1)", ".\(2)", ".\(3)", ".\(4)", ".\(5)", ".\(6)", ".\(7)", ".\(8)", ".\(9)"]]

override func viewDidLoad() {
    super.viewDidLoad()
    distancePicker.delegate = self
    distancePicker.dataSource = self

    goalTimePicker.delegate = self
    goalTimePicker.dataSource = self

    distanceLabel.textAlignment = .Center
    distanceLabel.numberOfLines = 2

    var tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    view.addGestureRecognizer(tap)
}

//MARK: UIPickerViewDataSource
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return pickerData.count
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData[component].count
}

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

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    updateLabel()
}

本质上,您需要检查哪个选择器正在调用您的委托方法。类似于:

//MARK: UIPickerViewDataSource
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
   if pickerView == goalTimePicker {
        return goalTimePickerData.count
   } else if pickerView == distancePicker {
        return pickerData.count
   }
   return 0
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
   if pickerView == goalTimePicker {
        // return rows for component for goal time picker
   } else if pickerView == distancePicker {
        // return rows of component for distance picker
   }    
}

//MARK: UIPickerViewDelegate
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
   if pickerView == goalTimePicker {
        // etc
   } else if pickerView == distancePicker {
        // etc
   }    
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
   if pickerView == goalTimePicker {
        // etc
   } else if pickerView == distancePicker {
        // etc
   }   
}