Multiple UIPickerView in the same UIView

后端 未结 5 1574
暖寄归人
暖寄归人 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:58

    I would suggest to define a variable in controller then decide the value based on field selection

    func textFieldDidBeginEditing(_ textField: UITextField) {
            if(textField == self.txtAge){
                **isAgeField=0**
                self.pickUp(txtAge)
            }else{
                **isAgeField=1**
                self.pickUpTimezone(timezonetxt)
            }
        }`
    
    then you can add condition inside the picker view as below 
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
           if isAgeField == 1 {
                return timeZoneList.count
            }else {
                return arrAge.count
            }
        }
        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
            if isAgeField == 1 {
                return timeZoneList[row]
            }else {
                return arrAge[row] as? String
            }
    
    
        }
        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            **if isAgeField == 1 {**
                timeZonepickerViewIndex = row
            }else  {
                selectedIndex = row
            }
    
        }
    

提交回复
热议问题