I have error with using UIPickerView
SelectViewController.swift:35:10: Method 'pickerView(pickerView:numberOfRowsInComponent:)' has different argument names from those required by protocol 'UIPickerViewDataSource' ('pickerView(_:numberOfRowsInComponent:)')
I set UIPickerView on Storyboard and attached this to the songPicker variable.
and then I think I integrated the functions necessary though, it showed error like this.
I found out that structure of picker view is changed on the version of swift.
However can't find the correct answer yet.
My swift is 3.1
Does anyone help me?
class SelectViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{
#IBOutlet weak var songPicker: UIPickerView!
let songNames = ["test","test2"]
override func viewDidLoad(){
songPicker.delegate = self
songPicker.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return songNames.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int) -> String? {
return songNames[row]
}
override func didReceiveMemoryWarning() {
}
}
try this code
class ElibraryViewController: UIViewController, UIPickerViewDelegate,UIPickerViewDataSource {
#IBOutlet weak var txtTitle: UITextField!
#IBOutlet weak var Picker: UIPickerView!
let songNames = ["test","test2"]
override func viewDidLoad()
{
super.viewDidLoad()
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return songNames[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
let obj = songNames[row]
txtTitle.text = obj
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return songNames.count
}
override func didReceiveMemoryWarning() {
}
}
Use this method, see the use of _ before pickerview. That is the only problem
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
}
UIPickerViewDataSource is changes in swift 3 and above.
Updated syntax:-
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return arrayData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return arrayData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
Have a look at apple documentation (SO reference).
I recommend you to use IQDropDownTextField - Click Here
Related
I believe I have followed the steps to implement a UIPickerView to display an array of strings. For some reason, the text from titleForRow is hidden. I seems like the picker view is getting the array count because I have the ability to scroll. I just can't get the list if names. I am using UIStoryboard and all outlets are connected. Any help would be appreciated.
extension SongPlayerViewController: UIPickerViewDataSource, UIPickerViewDelegate {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return hertzArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return hertzArray[row]
}
//when user selects row
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
}
class SongPlayerViewController: UIViewController {
let hertzArray = ["432Hz", "528Hz", "174Hz", "396Hz", "417Hz", "639Hz", "741Hz", "852Hz"]
#IBOutlet weak var pickerView: UIPickerView!
#IBOutlet weak var toolBar: UIToolbar!
//when view did load
override func viewDidLoad() {
super.viewDidLoad()
pickerView.dataSource = self
pickerView.delegate = self
}
}
I don't think there is a problem with the code you've posted. I put it into a playground with a little bit of setup code and it works. You should check your outlets in the nib file and check at runtime to ensure that the pickerView property is being assigned. As suggested in comments, check the dimensions of the picker using the view debugging tools
(https://developer.apple.com/library/archive/documentation/ToolsLanguages/Conceptual/Xcode_Overview/ExaminingtheViewHierarchy.html)
Here's the Playground I was working with:
import UIKit
import PlaygroundSupport
class SongPlayerViewController: UIViewController {
let hertzArray = ["432Hz", "528Hz", "174Hz", "396Hz", "417Hz", "639Hz", "741Hz", "852Hz"]
#IBOutlet weak var pickerView: UIPickerView!
override func loadView() {
let pickerView = UIPickerView()
self.pickerView = pickerView
view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
view.addSubview(pickerView)
pickerView.translatesAutoresizingMaskIntoConstraints = false
}
override func viewDidLoad() {
super.viewDidLoad()
pickerView.dataSource = self
pickerView.delegate = self
}
}
extension SongPlayerViewController: UIPickerViewDataSource, UIPickerViewDelegate {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return hertzArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return hertzArray[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
}
let songController = SongPlayerViewController()
PlaygroundSupport.PlaygroundPage.current.liveView = songController
I need some help. I've been looking for the solution for two days.
class Mam_sEntre_eViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
let notes = [0, 1, 2, 3, 4, 5]
#IBOutlet weak var noteGoutPickerView: UIPickerView!
#IBAction func validate() {
validateNoteMam_sEntre_e()
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return notes.count
}
private func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> Int? {
return notes[row]
}
private func validateNoteMam_sEntre_e() {
let noteGoutIndex = noteGoutPickerView.selectedRow(inComponent: 0)
let noteGout = notes[noteGoutIndex]
}
}
Change
private func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> Int? {
return notes[row]
}
To
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return String(notes[row])
}
Not private, return String not Int. See https://developer.apple.com/documentation/uikit/uipickerviewdelegate/1614384-pickerview
var Months = ["A","B","C","D"]
var pickerview = UIPickerView()
#IBOutlet var txt_Month: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
pickerview.isHidden = true
pickerview.dataSource = self
pickerview.delegate = self
txt_Month.inputView = pickerview
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return Months.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
txt_Month.text = Months[row]
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return Months[row]
}
maybe it's because pickerview is hidden?
Set the pickerview is unhide.
pickerview.isHidden = false
I am trying to get the second picker to display the array I created, but it won't and I have been messing with it for hours. Any ideas?
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return aircraft.count
}
#IBOutlet weak var aircraftType: UIPickerView!
#IBOutlet weak var model: UIPickerView!
let aircraft = ["Airbus", "Boeing", "Bombardier", "Embraer"]
let models = [ "Airbus": ["A300", "A320", "A330", "A350", "A380"], "Boeing": ["737", "747", "757/767", "777", "787"], "Bombardier": ["CRJ200", "CRJ700/900"], "Embraer": ["ERJ145", "ERJ170", "ERJ190"] ]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return aircraft [row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
Please find the updated code. You should reload pickerview component on select of Aircrafts. Do please take of optional bindings
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return aircraft.count
}
else {
return (models[aircraft[aircraftType.selectedRow(inComponent: 0)]]?.count)!
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return aircraft [row]
}
else {
return models[aircraft[aircraftType.selectedRow(inComponent: 0)]]?[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
aircraftType.reloadComponent(1)
}
}
I try to use this method selectedRow(inComponent: 0), yet it doesn't work for me. Any way out to get the first/default value from picker when it is not active?
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return countries.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return countries[row].name
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
country.text = countries[row].name //`country.text` is just textField
}
}
Not a big deal-
Just define your picker view as global and access the value for objectAtIndex(0)
And write this line of code in viewDidLoad()
Not exact, but something like this-
#IBOutlet var yourPicker: UIPickerView! = UIPickerView()
var yourArray = ["Apple", "Samsung", "Nokia"]
override func viewDidLoad() {
super.viewDidLoad()
yourPicker.text = yourArray[0]
}