Swift Picker项目

时间:2017-06-05 11:25:25

标签: swift xcode

我正在尝试让我的pickerView上显示的项目,我似乎无法弄清楚为什么它没有出现。有什么建议吗?

import UIKit

class InspectionViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {


    @IBOutlet weak var picker: UIPickerView!

    var pickerData: [String] = [String]();

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        // Connect data:
        self.picker.delegate = self;
        self.picker.dataSource = self;


        // Input data into the Array:
        pickerData = ["53W4", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"];
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

    }

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


    /*
     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */

}

1 个答案:

答案 0 :(得分:0)

你应该使用这个:func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return pickerData[row] }
而不是:
 func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { return pickerData[row] }