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
Related
i am trying to assign my pickerview data but what data type member is picker view?
when i use text it says the error "value of type uipickerview has no member text"
dateLabel.text = shiftItem.day
timing.text = shiftItem.slot
the ibaction is
#IBOutlet weak var timing: UIPickerView!
so for label or text field the data type would be text but what is it for pickerview?
this may seem like a dumb question but i could not find the answer anywhere
also, how can i pass the data a user has selected on a pickerview to another view controller?
thank you so much for all the help!
UIPickerView doesn't have any property named text. You have to use an array of String as it's datasource. Here is an example.
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var pickerView: UIPickerView!
let pickerData: [String] = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"]
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.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) {
// use the row to get the selected row from the picker view
// using the row extract the value from your datasource (array[row])
}
Please use the below code for it. Picker View doesn't have a text member.
class HomeController: UIViewController {
#IBOutlet weak var pickerView: UIPickerView!
var dataArray: [String] = ["Aman", "Raju", "Amit", "Aashish", "Sheshnath"]
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
// Use this method for initial text from your data array
pickerView.selectRow(1, inComponent: 0, animated: true)
}
}
extension HomeController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dataArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return dataArray[row]
}
}
Yes! I tried use "clip to bounds" bun in doesnt work, and i dont now why. Notice to gray areas on the sides
enter image description here
try this..
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var txtPicker: UITextField!
var testPickerView : UIPickerView!
var testArr = ["One","Two","Three","Four","Five"]
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
testPickerView = UIPickerView()
testPickerView.dataSource = self
testPickerView.delegate = self
testPickerView.backgroundColor = .green
testPickerView.layer.cornerRadius = 30.0
testPickerView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
testPickerView.clipsToBounds = true
txtPicker.inputAccessoryView = testPickerView
}
// MARK: pickerView
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return testArr.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
txtPicker.resignFirstResponder()
return testArr[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
txtPicker.text = testArr[row]
}
}
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've searched but I haven't found a solution to this, I have a UIPickerView and when I start view works fine, when I scrolling rows disappear
class SemanaViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIPickerViewDataSource, UIPickerViewDelegate {
let letters = ["A","B","C","D","F","G","H"]
#IBOutlet weak var pickerView: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
self.pickerView.dataSource = self
self.pickerView.delegate = self
....
}
....
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return letters.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return letters[row]
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let pickerLabel = UILabel()
//pickerLabel.textColor = UIColor.red
pickerLabel.text = letters[row]
return pickerLabel
}
Prints
any suggestion? thanks
I identified that the error was happening according to the way I was creating the views.
I have a UISegmentedControl and 2 subviews(UIViewController) and the error happened 1 subview(SemanalmenteViewController), I've changed how subviews are created this work's.
I dont know but I think it has to do with the Parent View Controller.
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