Home:ALL Converter>how to use UIPicker dataSource

how to use UIPicker dataSource

Ask Time:2016-06-29T15:35:01         Author:JhonSteve

Json Formatter

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).

Author:JhonSteve,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/38093163/how-to-use-uipicker-datasource
Yury :

Add UIPickerViewDataSource and UIPickerViewDelegate to the class declaration:\n\nclass ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate\n\n\nand don't use type casting using self:\n\noverride func viewDidLoad() {\n super.viewDidLoad()\n\n UIPicker.dataSource = self\n UIPicker.delegate = self\n}\n\n\nAlso you must move your methods out of viewDidLoad function, and correct them a bit:\n\nfunc numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {return 1}\n\nfunc pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {return Num.count}\n\nfunc pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {return Num[row]}\n\n\nNote 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\n\nUpd: full ViewController code:\n\nclass ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {\n\n let Num = [\"1\",\"2\",\"3\",\"4\",\"5\",\"6\"]\n\n @IBOutlet weak var UIPicker: UIPickerView!\n override func viewDidLoad() {\n super.viewDidLoad()\n\n UIPicker.dataSource = self\n UIPicker.delegate = self\n }\n\n func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {return 1}\n func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {return Num.count}\n func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {return Num[row]}\n}\n",
2016-06-29T07:44:50
yy