I cannot find a connection so that the outer class controls the view in the ViewController. I am new to iOS and have spent considerable attention on the solution. A simple example:
Subclass UIPickerView
I am creating a file that is a subclass of UIPickerView and is compatible with the PickerView delegate and data source.
class MyPickerView: UIPickerView, UIPickerViewDelegate, UIPickerViewDataSource {
Main view controller with output for PickerView
In my MainViewController, I create an output for my selection. Also, in StoryBoard, I plug in a โcustom classโ for my Picker View for MyPickerView above.
class MainViewController: UIViewController { @IBOutlet weak var myPickerView: UIPickerView! override func viewDidLoad() {
My questions:
---------------------
UPDATE: FINAL DECISION Enabling @Oscar Response
The @scscar suggestion below was great. To clarify, I would like my PickerView subclass to be a UIPickerView delegate, because Picker will always have the same interface and there are many PickerView delegate methods for the user interface. (attribitedTitleForRow, widthForComponent, rowHeightForComponent, etc.) I do not want to name these delegate methods in every ViewController that this PickerView uses.
Now that the PickerView "didSelectRow" is called, we need to notify our ViewController and pass the value that was selected. To make this work, I used a protocol. (summarized below). This topic took me some time to find out, but is crucial, so I suggest spending time with the protocols and delegation, if that makes no sense.
Create a protocol in PickerView using the func function that will be used to view the ViewControllers that represent this PickerView:
protocol MyPickerViewProtocol { func myPickerDidSelectRow(selectedRowValue:Int?) }
In the ViewController representing PickerView, it matches your PickerView protocol. In doing so, you need to place the func myPickerDidSelectRow somewhere in your ViewController:
class MyViewController: MyPickerViewProtocol { func myPickerDidSelectRow(selectedRowValue:Int?) {
@ Oscar's answer below will connect the picker view to your view controller, but there is one more thing. In order for PickerView to speak, you will need a property in your PickerView, which is a reference to the view controller in which it is contained. Here's the PickeView and ViewController classes in perspective:
Now that the row is selected in our PickerView, tell ViewController using our property: propertyThatReferencesThisViewController
class MyPickerView: UIPickerView {
source share