Hi there I have been trying to get this working for ages with no success so apologies if this seems easy for you guys.
I want to use a 3 wheel UIPickerView - The 1st wheel didSelectRow value will be used to change the array for the remaining two wheels but they will be the same as it is a conversion app.
It seems to throw me an error up when populating the wheels saying that Anyobject is not identical to String. I cannot see anything wrong but I know it is something basic I have missed.
Any pointers would be much appreciated.
Motty.
// ViewController.swift
// picker test
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate {
@IBOutlet weak var picker1Label: UILabel!
@IBOutlet weak var picker2Label: UILabel!
@IBOutlet weak var bigPicker: UIPickerView!
var wheelContents = []
var length = ["metres","feet","yards","inches","mm","cm","miles"]
var volume = ["m3","US Gall","Imp Gall","Barrels", "cubic FT","litres"]
var conType = ["length","volume"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
wheelContents = [conType,length,length]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//For main selection of type of conversion
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(bigPicker: UIPickerView) -> Int{
return wheelContents.count
}
// returns the # of rows in each component..
func pickerView(bigPicker: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return wheelContents[component].count
}
func pickerView(bigPicker: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
return wheelContents[component][row]
}
}
You need to tell Swift that your wheelContents
array is an array of array of String
:
var wheelContents:[[String]] = []
If you don't explicitly give wheelContents
a type, Swift infers it to be NSArray
which is what is giving you problems.
You need to say swift, that your wheelContents
is an array of String arrays:
var wheelContents:[[String]] = []
Also you should set the delegate of your pickerView
to self
because you handle the delegate in your class. Otherwise the functions won't work properly:
//In the viewDidLoad method
bigPicker.delegate = self
Since you declared wheelContents like this wheelContents = []
, without specifying its elements type, the compiler automatically infers that it is array of AnyObjects aka [AnyObject]
.
That's the reason why when you are returning wheelContents[component].count
it generates an error: at that moment the compiler is expecting a String!
but you are providing an AnyObject
.
It's a really easy fix, you should just specify what the content of the array it is going to be when you declare it. (it's an array of arrays of strings aka [[String]]
)
You need to set the datasource of pickerView along with the delegate in viewDidLoad
pickerView.dataSource = self
来源:https://stackoverflow.com/questions/28926570/using-uipickerview-with-multiple-components-in-swift