Search code examples
swiftxcodeswift2pickerdidselectrowatindexpath

How to use other class() delegates and datasource func in swift?


I have a UITextField class which confirms to following delegates. I'm using this class as a global class (its a small custom library, which I'm using as a spinner).

    //MARK: PickerView Delegate
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerDataArray![row]
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    self.text = pickerDataArray![row]
}

//MARK: TextField Delegate
func textFieldDidBeginEditing(textField: UITextField) {
    let row =  self.pickerView.selectedRowInComponent(0)
    self.text = self.pickerDataArray![row]
}

Now, I want to get the selected row, which in this class will be didSelectRow() func. So, how would I be able to use these functions in some way in those classes, where I implemented it?


Solution

  • You can do it using create Base class and extends in ChildClass or whenever you want to use

    example:

    SuperBaseViewController Class

    class SuperBaseViewController: UIPickerViewDataSource, UIPickerViewDelegate{
    
       //Add PickerView's Data Source and Data Delegate Methods Here
    
    }
    

    SubViewController Class

    class SubViewController: SuperBaseViewController{
    
       //Whenever you click on PickerView's row than it's delegate method in SuperBaseViewController if you add didSelect method
    
    }