我的UIPickerview输出可以与输入不同吗?

时间:2015-11-24 14:30:32

标签: ios iphone swift uipickerview uipicker

我有一个最终有4个值的选择器视图。有没有办法将项目保留在选择器视图中,但是它是否会为标签输出不同的值? 就像拾取器视图可以显示“4”但输出到标签“8”,例如?

到目前为止我的代码:

import UIKit

class GCSViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {

@IBOutlet var eyepicker: UIPickerView!
@IBOutlet var eyeoutput: UILabel!
let pickerData = ["1","2","3","4"]
override func viewDidLoad() {
    super.viewDidLoad()
    eyepicker.dataSource = self
    eyepicker.delegate = self
}

//mark: - Delegates and data sources
//MARK: Data Sources

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count
}
//MARK: Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerData[row]
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    eyeoutput.text = pickerData[row]
}
//The first method places the data into the picker and the second selects and display
}

1 个答案:

答案 0 :(得分:1)

选项1:

let pickerData = ["1","2","3","4"]
let outputData = ["2","4","6","8"]

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  eyeoutput.text = outputData[row]
}

选项2(使用猜测算法):

let pickerData = ["1","2","3","4"]

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  eyeoutput.text = "\(Int(pickerData[row])! * 2)"
}