UIAlertController 中的不同 UIPickerView Delegate/DataSource
Different UIPickerView Delegate/DataSource in UIAlertController
请告诉我如何使两个不同的 UIPickerView 具有不同的 delegate/datasource?我有两个单元格的 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 创建 class 并在我的 ViewController 中调用他?我不知道。提前致谢:]
当您说您希望选择器具有不同的委托时,您是在暗示不同的 classes 负责处理委托方法。因此,以下内容将解决您的问题:
picker.delegate = self
picker2.delegate = anotherClassInstance
但我认为这不是你要问的。我想你是在问如何让两个不同的选择器和不同的数据都由同一个 class 处理,从而共享同一个委托 class。最简单的方法是在每个选择器上分配标签 属性:
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
}
}
好的,我用 UIPickerDelegate/DataSource 创建了新的 class 并创建了这个 class 的新实例。我的代码如下所示:
委托 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
}
}
}
主要代码:
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
}
有效:]
请告诉我如何使两个不同的 UIPickerView 具有不同的 delegate/datasource?我有两个单元格的 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 创建 class 并在我的 ViewController 中调用他?我不知道。提前致谢:]
当您说您希望选择器具有不同的委托时,您是在暗示不同的 classes 负责处理委托方法。因此,以下内容将解决您的问题:
picker.delegate = self
picker2.delegate = anotherClassInstance
但我认为这不是你要问的。我想你是在问如何让两个不同的选择器和不同的数据都由同一个 class 处理,从而共享同一个委托 class。最简单的方法是在每个选择器上分配标签 属性:
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
}
}
好的,我用 UIPickerDelegate/DataSource 创建了新的 class 并创建了这个 class 的新实例。我的代码如下所示:
委托 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
}
}
}
主要代码:
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
}
有效:]