UIPickerView - 用于计算的值传输

时间:2015-08-04 08:29:47

标签: ios xcode swift

我很快就是新人。 可能有人可以帮助我。 我用自己的数据填充了一个pickerView。

我可以在数据后面存储一个值吗?

声明如下: 如果用户在pickerView上选择一些光圈,则存款价值(用户无法看到存款值)应该转到标签?进一步计算? 例如:开孔Ø6(沉积值为0.4)标签显示0,4 ccm

import Foundation

class Balkenbewehrung : UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

let pickerData = ["Ø 6","Ø 8","Ø 10","Ø 12","Ø 14","Ø 16","Ø 20",
    "Ø 25","Ø 26","Ø 28","Ø 30","Ø 32","Ø 36","Ø 40","Ø 50"]



@IBOutlet weak var myPicker: UIPickerView!
@IBOutlet weak var myLabel: UILabel!



override func viewDidLoad() {
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    myPicker?.delegate = self
    myPicker?.dataSource = self

}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}

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

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

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

感谢德国

3 个答案:

答案 0 :(得分:0)

您可以先进行计算,然后在UILabel

中指定其值
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
     // code your calculation here
      myLabel.text = yourResult
}

答案 1 :(得分:0)

我会尝试用Objective -c方式帮助你:

创建一个这样的类:

@interface PickerRowOption : NSObject

@property (nonatomic, retain) NSNumber * apertue;
@property (nonatomic, retain) NSNumber * deposit;

@end

使用从此类实例化的对象构建数据源数组。

NSArray * pickerData = // containts PickerRowOption s

做一些东西并返回光圈:

- (NSString *)pickerView:(UIPickerView *)pickerView
                       titleForRow:(NSInteger)row
                      forComponent:(NSInteger)component {
     PickerRowOption *option = [pickerdata objectAtIndex:row];

     //further calculations here....

     return option.aperture;
}

答案 2 :(得分:0)

使用预先计算的值构建数据结构并尝试此方法

    class ViewController : UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    @IBOutlet weak var myLabel: UILabel!
    @IBOutlet weak var myPicker: UIPickerView!

    var pickerDictionary: Dictionary<String, String> = ["Ø 6" : "0,4 ccm",
                                                        "Ø 8" : "0,6 ccm",
                                                        "Ø 10" : "0,8 ccm",
                                                        "Ø 12" : "0,10 ccm",
                                                        "Ø 14" : "0,12 ccm" ]

    override func viewDidLoad() {
        myPicker?.delegate = self
        myPicker?.dataSource = self
    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

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

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return pickerDictionary.keys.array[row]
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        myLabel.text = pickerDictionary[pickerDictionary.keys.array[row]]
    }
}