Multiple UIPickerView in the same UIView

后端 未结 5 1535
暖寄归人
暖寄归人 2021-02-01 20:24

I\'m a complete beginner on iOS dev and I want to create a little iOS application. On this application, 3 UIPickerViews are supposed to display different data.

5条回答
  •  面向向阳花
    2021-02-01 20:49

    You're comparing instances with tags.

    Replace:

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        if pickerView2 == 2 {
            return test[row]
        } else if pickerView3 == 3{
            return test2[row]
        }
            return ""
    }
    

    with:

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        if pickerView2.tag == 2 {
            return test[row]
        } else if pickerView3.tag == 3 {
            return test2[row]
        }
            return ""
    }
    

    Anyway, you don't need to tag your pickers. The pickers come in the datasource and delegate methods. You can compare instances. Something like this:

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        if pickerView2 == pickerView {
            return test[row]
        }
        if pickerView3 == pickerView {
            return test2[row]
        }
        return ""
    }
    

提交回复
热议问题