I don't know what's wrong with my code. I tried to follow the tutorial but same error happen.
Error:
Type 'FourthViewController' does not conform to protocol 'UIPickerViewDataSource'
Here is my code:
import UIKit
let characters = ["Jaja Bink", "Luke", "Han Solo", "Princess Leia"];
let weapons = ["LightSaber", "Pistol", "Keris"];
class FourthViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var doublePicker: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(pickerView: UIPickerView,
titleForRow row: Int,
forComponent component: Int) -> String? {
if component == 0 {
return characters[row]
} else {
return weapons[row]
}
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int? {
if component == 0 {
return characters.count
} else {
return weapons.count
}
}
}
Replacing deprecated version of the method of protocol UIPickerViewDataSource, if you're using Swift 3 could works for you.
Deprecated Method of Protocol
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
Latest Method of Protocol in Swift 3
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
Note: Same true for other required protocol methods.
i.e
Deprecated:
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int
Recent Version:
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
Because your required delegate method is not correct.
Replace it with this:
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return characters.count
} else {
return weapons.count
}
}
Your delegate method returns Int? which is not correct.
You have to implement it's function:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
Related
I am new with programming in Swift. i got problem when i want to show up 2 different picker with 2 different content. But it show (?) instead of content.
see the screenshoot below
Thank your for help!
let fruit = ["apple", "orange", "watermelon", "banana", "peach", "strawberry"]
let food = ["rice","bread","spagheti","milk"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView.tag == 1) {
return fruit.count
}
else {
return food.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView.tag == 1) {
return fruit[row]
}
else {
return food[row]
}
}
First of all check you pickerView DataSource and Delegate connected properly and then you can conditionally load data by comparing pickerView like below.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == YOUR_FIRST_PICKER {
return fruit.count
}
else {
return food.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == YOUR_FIRST_PICKER {
return fruit[row]
}
else {
return food[row]
}
}
I think you haven’t set your pickers’ delegates and data sources.
First add these protocols to your ViewController subclass
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
then in viewDidLoad set delegates and data sources of your pickers as self
override func viewDidLoad() {
super.viewDidLoad()
pickerView1.delegate = self
pickerView1.dataSource = self
pickerView2.delegate = self
pickerView2.dataSource = self
}
I need to give title for rows according to array but I don't know how to do this. I'm new to iOS.
class ViewController: UIViewController , UIPickerViewDelegate, UIPickerViewDataSource{
#IBOutlet weak var pickerView: UIPickerView!
#IBOutlet weak var label: UILabel!
var names: Array<String>?
var department: Array<String>?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
names = ["Vineeta", "Shubham" ,"Anand", "Vivek", "Akshay", "Harshit", "Vikas", "Prasoon", "Sunil"]
department = ["iOS","Angular", ".Net", "iOS","iOS", ".Net", "Android","Android", "Angular"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 9
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
[names o]
}
}
}
don't forget UIPickerViewDelegate,UIPickerViewDataSource
#IBOutlet weak var picker: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
pickerData = ["X","Y"]
picker.delegate = self
picker.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let selectedText = pickerData[row]
}
Use this code -
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
if component == 0
{
return names?.count
}
else
{
return department?.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
if component == 0
{
return names?[row]
}
else
{
return department?[row]
}
}
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
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.
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]
}
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.
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
}
You are missing this from the delegate:
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
I've defined a picker which does not return always the right value. Sometimes it just works but most of the time when I pick the item pickerView returns the wrong value.
I did associate the outlet delegate to the view controller and am using the following code:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate {
var categories = ["animals", "profession", "sea_animals", "food", "tools", "misc", "all"]
var cat: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
println("cat size: \(categories.count)")
return categories.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
println("cat: \(categories[row])")
return categories[row]
}
}
it works sometime but it is not reliable.
Any idea what I do wrongly?
Thanks!
I found the problem.
There is a function for selecting the row :)
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
I am new to programming, so please forgive the basic nature of this question.
I am trying to put multiple picker views into the same class, but they keep coming back with the same choices.
class ViewController: UIViewController, UIPickerViewDelegate{
#IBOutlet var outgoing: UIPickerView!
var var1=["1","2"]
var var2=["a","b"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfComponentsInPickerView(pickerView: UIPickerView!) ->Int{
return 2
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) ->Int {
return var1.count
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int)->String!{
return var2[row]
}
Any suggestions?
For displaying two components you need to change your methods like:
func numberOfComponentsInPickerView(pickerView: UIPickerView!) ->Int
{
return 2
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) ->Int
{
var rows = var2.count
if component == 0
{
rows = var1.count
}
return rows;
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int)->String!
{
var componentStr = var2[row]
if component == 0
{
componentStr = var1[row]
}
return componentStr;
}