0

Im trying to have two different pickerViews on the same viewController. And have them represent different data. I have tried a couple different ways to do this and seen couple of similar situations to mine on here, but can't put it all together. Any help would be awesome!

Here is all relevant code:

@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()
}

1 Answer 1

3

Essentially, you need to check which picker is calling your delegate methods. Something like:

//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
   }   
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch! I was so close, Im still not great at translating from obj-C to swift, and thats what I was trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.