skip to Main Content

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!

3

Answers


  1. 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]
        }
    }
    
    Login or Signup to reply.
  2. 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])
    }

    Login or Signup to reply.
  3. 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]
    }
    }
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search