Search code examples
iosswiftuipickerview

number input with UiPickerView


In my use case I would like to add in numbers consisting of 4 digits, like in the image. The first digit should be 0 or 1.

My code so far is:

func numberOfComponents(in weightPickerView: UIPickerView) -> Int {
    return 4
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return loopingMargin * numbers.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return numbers[row % numbers.count]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    digits[component] = row % numbers.count
    let weightString = "\(digits[0])\(digits[1])\(digits[2])\(digits[3])"
    weightField.text = weightString
}

How can I achieve that?

enter image description here


Solution

  • You can try

     func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
         if(component == 0)
         {
            return 2
         }
         else
         {
           return loopingMargin * numbers.count
         }
    
     }