如何使用UIPicker dataSource

时间:2016-06-29 07:35:02

标签: swift

我使用的是Xcode 8和swift 2.3,但是当我尝试使用UIPickerView.dataSource = (self as! UIPickerViewDataSource)的func时,我遇到了UIPicker无法解决的问题,这是我的代码:

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

当我运行我的代码时,Xcode警告代码:

  

无法转换类型' test.ViewController'的值(0x1000bd8c0)到   ' UIPickerViewDataSource' (0x1ab289738)。 2016-06-29 15:33:35.079300   test [474:98176]无法转换类型' test.ViewController'   (0x1000bd8c0)到' UIPickerViewDataSource' (0x1ab289738)。

2 个答案:

答案 0 :(得分:5)

UIPickerViewDataSourceUIPickerViewDelegate添加到类声明:

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate

并且不要使用self进行类型转换:

override func viewDidLoad() {
    super.viewDidLoad()

    UIPicker.dataSource = self
    UIPicker.delegate = self
}

此外,您必须将方法移出viewDidLoad函数,并稍微纠正它们:

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

请注意,我强烈建议您将NumUIPicker属性重命名为numpicker,这样就不会将它们与类名混合

更新:完整ViewController代码:

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

答案 1 :(得分:1)

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]

    }


}