我的 UIPickerview 输出可以与输入不同吗?
Can my UIPickerview output be different that the input?
我有一个最终会有 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:
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)"
}
我有一个最终会有 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:
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)"
}