UIAlertController中的UIPickerView Delegate / DataSource不同

时间:2016-04-15 13:55:12

标签: ios swift uipickerview uialertcontroller uipicker

请告诉我如何使用不同的委托/数据源制作两个不同的UIPickerView?我有两个单元格的TableView,我需要用UIPickerView打开两个不同的UIAlertController。我的代码:

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)
}

我有实现UIPickerViewDelegate和UIPickerViewDataSource。我需要在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
    }
}

}

我可能需要使用UIAlertController创建类并在我的ViewController中调用它吗?我不知道。提前谢谢你:]

2 个答案:

答案 0 :(得分:0)

当你说你希望选择器有不同的委托时,你建议不同的类负责处理委托方法。因此,以下内容将解决您的问题:

picker.delegate = self
picker.tag = 1
picker2.delegate = self  
picker2.tag = 2

但我不认为那就是你所要求的。我想你问的是如何让两个不同数据的不同数据都被同一个类处理,从而共享同一个委托类。最简单的方法是在每个选择器上分配tag属性:

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

       switch row {
        case 0:
            return "None"
        case 1:
            return "Other picker data"
        default:
            return nil
    }
}

然后,区分它们:

{{1}}

答案 1 :(得分:0)

好的,我使用UIPickerDelegate / DataSource创建了新类,并为此类创建了新实例。我的代码看起来像这样:

委托课程:

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
        }
    }
}

主要代码:

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
}

它起作用:]