1

我有以下代码。没有错误。应用程序崩溃。不确定我是否正确初始化。请,非常感谢任何帮助。

@IBOutlet weak var myCostPicker: UIPickerView!

var costCode = String()

override func viewDidLoad() {
    super.viewDidLoad()
    if let projectNum = projectNum {
        projectNumTitle.title = projectNum

    }

    updateLabels()
    myCostPicker.dataSource = self
    myCostPicker.delegate = self
    myCostPicker.showsSelectionIndicator = true
    self.view.backgroundColor = UIColor.blackColor()
}

let pickerData = ["Nothing", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q"]

let costCodeData = ["Nothing", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q"]

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData.count
}

// MARK: - Picker Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String {
    return pickerData[row]
}

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let titleData = pickerData[row]
    var myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
    return myTitle
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    myLabel.text = pickerData[row]
    costCode = costCodeData[row]
}

达到了心理障碍。需要有人来敲打我的脑袋。赞赏。

4

1 回答 1

2

如果我理解您的问题是正确的,那么您似乎在情节提要或 XIB 中完成了选择器设置,但选择器似乎没有填充 pickerData 数组中的数据。

我尝试了您的代码并且它运行得很好,它可能是您可能没有完成的 IBOutlet 链接到正确的视图控制器类,另外,如果您使用的是故事板/XIB 方法,请尝试从 .storyboard 添加数据源和委托或 .xib 文件本身

在此处输入图像描述

或者

由于一些有趣的原因,pickerData 可能会被刷新,您可以使用调试器进行检查。

如果所有设置都正确完成,然后您也无法看到最终输出,那么有时当您创建新的可可触摸类文件时,它们没有正确创建,因此您删除它们并重新创建它们(我知道真的很愚蠢,但它有时真的有效)

于 2015-03-20T08:44:00.277 回答