我制作了一个可以转换测量单位的计算器。该应用程序总是向我显示第一个(km)的答案,除非整个答案。我希望你们中的一个人能找到我的错误。
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet var label: UILabel!
@IBOutlet var Picker1: UIPickerView!
@IBOutlet var num: UITextField!
var array1 = ["km","m","dm","mm"]
var PlaceAnswer = 0
var answer = Float()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return array1[row]
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return array1.count
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
@IBAction func Submit(sender: AnyObject) {
if(PlaceAnswer==0){
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .DecimalStyle
let number = numberFormatter.numberFromString(num.text!)
answer = number!.floatValue
answer = answer * 0.0001
label.text = ("\(answer)"+"km")
answer = 0
}
else if(PlaceAnswer==1)
{
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .DecimalStyle
let number = numberFormatter.numberFromString(num.text!)
answer = number!.floatValue
answer = answer * 100
label.text = ("\(answer)" + "m")
answer = 0
}
else if(PlaceAnswer==2)
{
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .DecimalStyle
let number = numberFormatter.numberFromString(num.text!)
answer = number!.floatValue
answer = answer * 10
label.text = ("\(answer)" + "dm")
answer = 0
}
else
{
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .DecimalStyle
let number = numberFormatter.numberFromString(num.text!)
answer = number!.floatValue
answer = answer / 10
label.text = ("\(answer)"+"mm")
answer = 0
}
}
}
答案 0 :(得分:0)
因为PlaceAnswer
的值在代码中的任何位置都没有变化。
使用此项设置您的值
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
PlaceAnswer = row
}