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

标签 ios iphone swift uipickerview uipicker

我有一个选择器 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/

相关文章:

ios - UILabel高度计算

iphone - TWTweetComposeViewController 不允许编辑

ios - Iphone 肖像模式切换回风景模式无法自动工作

iphone - UINavigationController 不显示 Root View Controller

ios - 在启用换行模式的 NSLayoutManager 中获取正确的字形点

swift - 在 Swift 2 中发起 HTTP POST 请求

iphone - UITabBarController,有什么可能的方法可以通过水平滑动移动下一个 View Controller?

ios - 保存包括条形图在内的完整屏幕截图

ios - 我在使用 npm run ios 命令时遇到错误

swift - 如何声明泛型基类?