我有以下代码。没有错误。应用程序崩溃。不确定我是否正确初始化。拜托,非常感谢任何帮助。
@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]
}
达到心理障碍。需要有人来讨厌我的头脑。赞赏。
答案 0 :(得分:2)
如果我理解你的问题是正确的,似乎你在故事板或XIB中完成了选择器设置,但是选择器似乎没有填充pickerData数组中的数据。
我尝试了你的代码并且它的工作非常好,它可能是你可能没有对正确的视图控制器类进行IBOutlet的链接。另外,如果你使用的是storyboard / XIB方法,请尝试添加数据源和委托自.storyboard或.xib文件本身
OR
由于一些有趣的原因,pickerData可能会被刷新,你可以使用调试器检查它。
如果所有设置都正确完成,那么你也无法看到最终输出,那么有时当你创建一个新的cocoa touch类文件时,它们的创建不正确,所以你删除它们并重新创建它们(我知道真实的愚蠢,但它确实有效)