SpectralDragonChan SpectralDragonChan - 7 months ago 109
Swift Question

Different UIPickerView Delegate/DataSource in UIAlertController

Please tell me how make two different UIPickerView with different delegate/datasource? I have TableView with two cells, and i need open two different UIAlertController with UIPickerView. My code:

private weak var filterController: UIAlertController! {

let controllerConfig = UIAlertController(title: "", message: "\n\n\n\n\n\n\n\n\n", preferredStyle: .Alert)

controllerConfig.modalInPopover = true

//Create UIPickerView
let pickerFrame = CGRectMake(0, 20, 270, 180)
let picker = UIPickerView(frame: pickerFrame)
//Picker Color
picker.backgroundColor = controllerConfig.view.backgroundColor

//Picker Delegate/DataSource
picker.delegate = self
picker.dataSource = self

//Add Picker
controllerConfig.view.addSubview(picker)

//Create header frame
let headerFrame = CGRectMake(0, 5, 270, 45)
let headerView = UIView(frame: headerFrame)
headerView.backgroundColor = controllerConfig.view.backgroundColor

//Create 'Close' button
var closeButton: UIButton! {
let buttonFrame = CGRectMake(230, 5, 35, 35)
let buttonConfig = UIButton(frame: buttonFrame)
buttonConfig.setTitle("X", forState: .Normal)
//Set color on text
buttonConfig.setTitleColor(UIColor.grayColor(), forState: .Normal)
buttonConfig.setTitleColor(erablagoThemeColor, forState: .Highlighted)
buttonConfig.addTarget(self, action: #selector(didClickOnCloseButton), forControlEvents: .TouchUpInside)

return buttonConfig
}

let showAction = UIAlertAction(title: "Select", style: .Default) { (action) in
//TODO
}

//Add subview
headerView.addSubview(closeButton)
controllerConfig.addAction(showAction)
controllerConfig.view.addSubview(headerView)

return controllerConfig
}

///Close current view
func didClickOnCloseButton() {
dismissViewControllerAnimated(true, completion: nil)
}


And i have implementation UIPickerViewDelegate and UIPickerViewDataSource. I need change this implementation in my ViewContriller.

//MARK: - UIPickerViewDataSource
extension FilterController: UIPickerViewDataSource {
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 2
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch row {
case 0:
return "All"
case 1:
return "Cars and Bicycles"
default:
return nil
}
}


}

May be me need create class with UIAlertController and call him in my ViewController? I don't know. Thank you in advance:]

Answer

Ok, I made new class with UIPickerDelegate/DataSource and made new instance this class. My code looks like that:

Delegate class:

class SortFilterPicker: NSObject, UIPickerViewDelegate, UIPickerViewDataSource {

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 4
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch row {
        case 0:
            return "1-1"
        case 1:
            return "2-2"
        case 2:
            return "3-3"
        case 3:
            return "4-4"
        default:
            return nil
        }
    }
}

And main code:

private let categoryFilter = FilterPicker()

private weak var filterController: UIAlertController! {

    var heightPopUp = "\n"
    let controllerConfig = UIAlertController(title: "", message: heightPopUp.repeatOf(9), preferredStyle: .Alert)

    controllerConfig.modalInPopover = true
    let pickerFrame = CGRectMake(0, 20, 270, 180)
    let picker = UIPickerView(frame: pickerFrame)

    picker.delegate = categoryFilter
    picker.dataSource = categoryFilter

    picker.backgroundColor = controllerConfig.view.backgroundColor
    controllerConfig.view.addSubview(picker)

    let headerFrame = CGRectMake(0, 5, 270, 45)
    let headerView = UIView(frame: headerFrame)
    headerView.backgroundColor = controllerConfig.view.backgroundColor

    var closeButton: UIButton! {
        let buttonFrame = CGRectMake(230, 5, 35, 35)
        let buttonConfig = UIButton(frame: buttonFrame)
        buttonConfig.setTitle("X", forState: .Normal)
        //Set color on text
        buttonConfig.setTitleColor(UIColor.grayColor(), forState: .Normal)
        buttonConfig.setTitleColor(erablagoThemeColor, forState: .Highlighted)
        buttonConfig.addTarget(self, action: #selector(didClickOnCloseButton), forControlEvents: .TouchUpInside)

        return buttonConfig
    }

    let showAction = UIAlertAction(title: "Ok", style: .Default) { (action) in
        //TODO: Make show
    }

    headerView.addSubview(closeButton)
    controllerConfig.addAction(showAction)
    controllerConfig.view.addSubview(headerView)

    return controllerConfig
}

It's works :]

Comments