简单的UIPickerView错误

时间:2016-03-22 20:35:04

标签: ios swift uipickerview

我在使用myPicker.dataSource = self的行上收到以下错误。有谁知道为什么这是或我可以做些什么来解决它?

  

威胁1:EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码= 0x0)

以下是代码:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    @IBOutlet var myPicker: UIPickerView!
    @IBOutlet var textFIled: UITextField!

    let pickerData = ["Mozzarella","Gorgonzola","Provolone","Brie","Maytag Blue","Sharp Cheddar","Monterrey Jack","Stilton","Gouda","Goat Cheese", "Asiago"]

    override func viewDidLoad() {
        super.viewDidLoad()
        myPicker.dataSource = self
        myPicker.delegate = self
    }

    func numberOfComponentsInPickerView(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) {
        textFIled.text = pickerData[row]
    }
}

2 个答案:

答案 0 :(得分:0)

当您的IBOulet未正确连接到故事板中的UIPickerView时,会发生这种情况。删除所有连接并再次连接。

答案 1 :(得分:0)

首先,确保故事板中已连接myPickerView。检查故事板中的插座名称是否相同以及ViewController

如果没有运气,请尝试通过故事板将故事板UIPickerView连接到委托和数据源。右键单击并从选择器视图拖动到视图控制器,然后选择“委托”和“dataSource”。