UIAlertController 中不同的 UIPickerView Delegate/DataSource

Posted

技术标签:

【中文标题】UIAlertController 中不同的 UIPickerView Delegate/DataSource【英文标题】:Different UIPickerView Delegate/DataSource in UIAlertController 【发布时间】:2016-04-15 13:55:11 【问题描述】:

请告诉我如何使用不同的委托/数据源制作两个不同的 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 中调用他吗?我不知道。提前谢谢你:]

【问题讨论】:

嗯,我可以创建新的委托类。可能对我有帮助。 【参考方案1】:

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

picker.delegate = self
picker2.delegate = anotherClassInstance

但我认为这不是你要问的。我想您是在问如何让两个具有不同数据的不同选择器都由同一个类处理,从而共享同一个委托类。最简单的方法是在每个选择器上分配标签属性:

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

然后,区分它们:

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
    

【讨论】:

谢谢!我会尝试使用它,稍后我会告诉你它是否有效:)【参考方案2】:

好的,我用 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

它的作品:]

【讨论】:

以上是关于UIAlertController 中不同的 UIPickerView Delegate/DataSource的主要内容,如果未能解决你的问题,请参考以下文章

UIAlertController 中不同的 UIPickerView Delegate/DataSource

从 UINavigationItem 扩展中呈现 UIAlertController

为啥这个 UIAlertController 不显示?

UIAlertController更改字体颜色

iOS8统一的系统提示控件——UIAlertController

多个 UIAlertController 在 Swift 中一个接一个地显示