how to use UIPicker dataSource - swift

I use the Xcode 8 and swift 2.3, but I met a problem the UIPickerView.dataSource = (self as! UIPickerViewDataSource) doesn't work when I try to use func of UIPicker, this is my code:
import UIKit
class ViewController: UIViewController {
let Num = ["1","2","3","4","5","6"]
#IBOutlet weak var UIPicker: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
func numberOfRows(inComponent component: Int) -> Int {return 1}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {return Num.count}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {return Num[row]}
UIPicker.dataSource = (self as! UIPickerViewDataSource)
UIPicker.delegate = (self as! UIPickerViewDelegate)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
When I run my code, Xcode warning the code:
Could not cast value of type 'test.ViewController' (0x1000bd8c0) to
'UIPickerViewDataSource' (0x1ab289738). 2016-06-29 15:33:35.079300
test[474:98176] Could not cast value of type 'test.ViewController'
(0x1000bd8c0) to 'UIPickerViewDataSource' (0x1ab289738).

Add UIPickerViewDataSource and UIPickerViewDelegate to the class declaration:
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate
and don't use type casting using self:
override func viewDidLoad() {
super.viewDidLoad()
UIPicker.dataSource = self
UIPicker.delegate = self
}
Also you must move your methods out of viewDidLoad function, and correct them a bit:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {return 1}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {return Num.count}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {return Num[row]}
Note that I highly recommend you to rename your Num and UIPicker properties to num and picker, so you will not mix them with class names
Upd: full ViewController code:
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
let Num = ["1","2","3","4","5","6"]
#IBOutlet weak var UIPicker: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
UIPicker.dataSource = self
UIPicker.delegate = self
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {return 1}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {return Num.count}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {return Num[row]}
}

import Foundation
import UIKit
class MakeReferralViewController: UITableViewController,UIPickerViewDelegate, UITextFieldDelegate,UIPickerViewDataSource {
#IBOutlet weak var makeReferralScroll: UIScrollView!
#IBOutlet weak var userTitleUITextView: UITextField!
var pickerData = ["Mr.", "Ms.", "Mrs.", "Miss."];
var picker = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
userTitleUITextView.inputView = picker
// 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 textFieldDidBeginEditing(_ textField: UITextField) {
makeReferralScroll.setContentOffset(CGPoint(x:0,y:250), animated: true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true;
}
#available(iOS 10.0, *)
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
makeReferralScroll.setContentOffset(CGPoint(x:0,y:0), animated: true)
}
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]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
userTitleUITextView.text = pickerData[row]
}
}

Related

value of type uipickerview has no member text

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]
}
}

How to clip this corners(on the screenshot) in UIPickerView?

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]
}
}

UIPickerView row text is hidden

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

UIPickerView scrolling rows disappear

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.

UIPickerView on Swift 3

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