如何同时分别控制两个选择器视图内容?

时间:2015-11-22 16:52:28

标签: ios swift uipickerview

two picker view with same data content

我想同时控制两个选择器视图并将其从一个转换为另一个。它是我在swift中的区域转换器应用程序。

这是我的代码,

@IBOutlet weak var Picker1: UIPickerView!
@IBOutlet weak var dataEntered: UITextField!
@IBOutlet weak var resultLabel: UILabel!

var inputArray = ["Hector","Bigha","Kattha","Ropani","Daam"]
var outputArray = ["just", "checking","to ", "add","another"]
var wheelContent = []

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return wheelContent.count
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return inputArray.count    
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return inputArray[row]
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    _ = inputArray[row]

}

@IBAction func convertButton(sender: AnyObject) {

    resultLabel.text = "hello"


}

2 个答案:

答案 0 :(得分:0)

在您的情况下,您可以使用两个隔间作为选择器视图,而不需要两个选择器视图。

$stampsdata     = json_decode($stampsjson, true);
$numberofstamps = $stampsdata['stamps']['stamp_id']);

如果你真的想要两个选择器视图,你可以在它自己的类中实现var dataSource:[String] = ["Hector","Bigha","Kattha","Ropani","Daam"] func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int { return 2 } func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int { return dataSource.count } func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! { return dataSource[row] } func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) { let from = dataSource[pickerView.selectedRowInComponent(0)] let to = dataSource[pickerView.selectedRowInComponent(1)] if value = Int(string:textField.text){ let result = Converter.convert(from: from, to: to, value: value) } } class Converter: NSObject { static func convert(from from: from, to: to, value: value) -> Float{ // do the conversion logic } } dataSource。这样,您可以在delegate

中实例化其中两个类

答案 1 :(得分:0)

在第一个选择器视图中选择某些内容时,您应该创建一个调用方法的转换类。您需要使用NS_ENUM或两个来强化它以将您的答案绑定到查询中,但如果您只是将NSString作为输入,则这是一种方法(a如果您的库是静态的并且清楚地枚举,则建议使用switch语句:

func conversionForString(stringToConvert: String) -> String? {
    if (stringToConvert == "Kattha") {
        return "Ropani"
    else if (stringToConvert == "Ropani") {
        return "Daam"
    } else {
        return "Kattha"
    }
}

在此之后,一旦获得转换后的字符串,您需要在第二个UIPickerView中查找它,并获取它所在的行。然后,调用此方法:

let selectedRow = 3 // assuming you found it at row 3
secondPickerView.selectRow(row: selectedRow, inComponent: selectedComponent, animated: true)