我有一个选择器 View ,最终将有 4 个值。有没有办法将项目保留在选择器 View 中,但让它向标签输出不同的值? 例如,选择器 View 可以显示“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:
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)"
}
关于ios - 我的 UIPickerview 输出可以与输入不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33896261/