我有4个.plist文件,我想用它来通过滑块填充UIPickerView,现在我可以在ViewDidLoad方法中填充第一个文件,即" WeightUnits"之一。
现在滑块在加载视图时从1开始具有1到4的值,但是当我向右滑动(值2)时,它应该使用下一个.plist文件重新填充UIPickerView但是由于某种原因它会崩溃,我想我知道原因所在,但我无法找到解决方案。 (我在代码中添加了评论)
请注意: UIPickerView连接到控制器委托和数据源
提前感谢任何帮助/解决方案
导入UIKit
类ConverterScript:UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
fileprivate let convertFromComponent = 0
fileprivate let convertToComponent = 1
fileprivate var convertFromTo:[String : [String]]!
fileprivate var convertFrom:[String]!
fileprivate var convertTo:[String]!
@IBOutlet weak var sliderLabel: UILabel!
@IBOutlet weak var converterPicker: UIPickerView!
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var valueField: UILabel!
@IBOutlet weak var resultUnitOfMeasure: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
sliderLabel.text = "Weight"
let bundle = Bundle.main
let plistURL = bundle.url(forResource: "WeightUnits", withExtension: ".plist")
convertFromTo = NSDictionary(contentsOf: plistURL!) as! [String: [String]]
let allWeights = convertFromTo.keys
convertFrom = allWeights.sorted()
let selectedWeight = convertFrom[0]
convertTo = convertFromTo[selectedWeight]
converterPicker.reloadComponent(convertFromComponent)
converterPicker.reloadComponent(convertToComponent)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func ifSliderChanges(_ sender: UISlider) {
sliderLabel.text = "\(lroundf(sender.value))"
if (sender.value < 1.5) {
sliderLabel.text = "Weight"
let bundle = Bundle.main
let plistURL = bundle.url(forResource: "WeightUnits", withExtension: ".plist")
convertFromTo = NSDictionary(contentsOf: plistURL!) as! [String: [String]]
let allWeights = convertFromTo.keys
convertFrom = allWeights.sorted()
let selectedWeight = convertFrom[0]
convertTo = convertFromTo[selectedWeight]
converterPicker.reloadComponent(convertFromComponent)
converterPicker.reloadComponent(convertToComponent)
converterPicker.selectRow(0, inComponent: convertFromComponent, animated: true)
converterPicker.selectRow(0, inComponent: convertToComponent, animated: true)
} else if (sender.value >= 1.5 && sender.value <= 2.5){
sliderLabel.text = "Volume"
let bundle = Bundle.main
let plistURL = bundle.url(forResource: "VolumeUnits", withExtension: ".plist")
//issue starts here according to Xcode Debugging!!!!!
convertFromTo = NSDictionary(contentsOf: plistURL!) as! [String: [String]]
let allWeights = convertFromTo.keys
convertFrom = allWeights.sorted()
let selectedWeight = convertFrom[0]
convertTo = convertFromTo[selectedWeight]
converterPicker.reloadComponent(convertFromComponent)
converterPicker.reloadComponent(convertToComponent)
converterPicker.selectRow(0, inComponent: convertFromComponent, animated: true)
converterPicker.selectRow(0, inComponent: convertToComponent, animated: true)
}else if (sender.value >= 2.5 && sender.value <= 3.5){
sliderLabel.text = "Length"
let bundle = Bundle.main
let plistURL = bundle.url(forResource: "LengthUnits", withExtension: ".plist")
convertFromTo = NSDictionary(contentsOf: plistURL!) as! [String: [String]]
let allWeights = convertFromTo.keys
convertFrom = allWeights.sorted()
let selectedWeight = convertFrom[0]
convertTo = convertFromTo[selectedWeight]
converterPicker.reloadComponent(convertFromComponent)
converterPicker.reloadComponent(convertToComponent)
converterPicker.selectRow(0, inComponent: convertFromComponent, animated: true)
converterPicker.selectRow(0, inComponent: convertToComponent, animated: true)
}
else if (sender.value > 3.5){
sliderLabel.text = "Temperature"
let bundle = Bundle.main
let plistURL = bundle.url(forResource: "TemperatureUnits", withExtension: ".plist")
convertFromTo = NSDictionary(contentsOf: plistURL!) as! [String: [String]]
let allWeights = convertFromTo.keys
convertFrom = allWeights.sorted()
let selectedWeight = convertFrom[0]
convertTo = convertFromTo[selectedWeight]
converterPicker.reloadComponent(convertFromComponent)
converterPicker.reloadComponent(convertToComponent)
converterPicker.selectRow(0, inComponent: convertFromComponent, animated: true)
converterPicker.selectRow(0, inComponent: convertToComponent, animated: true)
}
}
@IBAction func onButtonPressed(_ sender: UIButton) {
let convertFromRow = converterPicker.selectedRow(inComponent: convertFromComponent)
let convertToRow = converterPicker.selectedRow(inComponent: convertToComponent)
let from = convertFrom[convertFromRow]
let to = convertTo[convertToRow]
if valueField.text?.isEmpty ?? true {
let title = "error"
let message = "please enter a value"
let alert = UIAlertController(
title: title,
message: message,
preferredStyle: .alert)
let action = UIAlertAction(
title: "OK",
style: .default,
handler: nil)
alert.addAction(action)
present(alert, animated: true, completion: nil)
} else{
if (slider.value < 1.5) {
} else if (slider.value >= 1.5 && slider.value <= 2.5){
}else if (slider.value >= 2.5 && slider.value <= 3.5){
}
else if (slider.value > 3.5){
}
}
resultUnitOfMeasure.text = "\(to)"
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == convertFromComponent {
return convertFrom.count
} else {
return convertTo.count
}
}
//Picker View Delegate Method
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == convertFromComponent {
return convertFrom[row]
} else {
return convertTo[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == convertFromComponent {
let selectedWeight = convertFrom[row]
convertTo = convertFromTo[selectedWeight]
converterPicker.reloadComponent(convertToComponent)
}
}
}