Reputation: 65
import UIKit
class FourthViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
@IBOutlet weak var picker: UIPickerView!
var pickerData: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.picker.delegate = self
self.picker.dataSource = self
pickerData = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"] }
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - Delegates and data sources
//MARK: Data Sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
//MARK: Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Error:Type 'FourthViewController' does not conform to protocol 'UIPickerViewDataSource'
Just for testing some features but i dont get the problem
Upvotes: 4
Views: 3564
Reputation: 99
You are missing this from the delegate:
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
Upvotes: 0
Reputation: 998
I got error due to the delegate method
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
if pickerView == stylePickerView {
return 1 }
else if pickerView == fontPickerView {
return 1 }
return 0
}
tried this instead and worked
@available(iOS 2.0, *)
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
if pickerView == stylePickerView {
return 1 }
else if pickerView == fontPickerView {
return 1 }
return 0
}
Upvotes: 0
Reputation: 416
You must conform data source protocol:
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
Try this code:
//MARK: - Delegates and data sources
//MARK: Data Sources
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
//MARK: Delegate
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
Upvotes: 4
Reputation: 47896
As you have found, you need to implement numberOfComponents(in:)
.
Your numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
, needs to be changed to:
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
Also you need to modify your pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
In Swift 3 many methods are renamed. Check the latest reference, and be careful about this. And you better remark Xcode version in your question.
Upvotes: 11
Reputation: 27448
You have to implement all the required methods of UIPickerViewDataSource
and UIPickerViewDelegate
if you confirming that protocol in swift
. otherwise it will give error at compile time.
Upvotes: 1