简体   繁体   English

快速将选择器的委托和数据源外包给另一个类

[英]Outsourcing the delegate and datasource of a picker to another class in swift

I am creating more than one picker in one view controller, so I have to use separate cocoa touch classes to handle the datasource and delegate methods of each of them. 我要在一个视图控制器中创建多个选择器,所以我必须使用单独的可可触摸类来处理每个数据源的数据源和委托方法。 I have this code: 我有以下代码:

myPicker.delegate = CustomPickerViewController

myPicker.dataSource = CustomPickerViewController

In this case, CustomPickerViewController is a cocoa touch class in a separate file which has the required UIPickerViewDelegate and UIPickerViewDataSource methods implemented. 在这种情况下, CustomPickerViewController是一个单独文件中的可可接触类,该文件具有所需的UIPickerViewDelegateUIPickerViewDataSource方法。 So, what should I be assigning to the above properties in order to handle populating a picker view using the CustomPickerViewController class? 因此,我应该为上述属性分配什么以便使用CustomPickerViewController类处理填充选择器视图?

Each picker should get its own instance of CustomPickerViewController . 每个选择器都应获取自己的CustomPickerViewController实例。

myPicker.delegate = CustomPickerViewController()
myPicker.dataSource = myPicker.delegate

anotherPicker.delegate = CustomPickerViewController()
anotherPicker.dataSource = anotherPicker.delegate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM