用数字与数字相乘的标签

时间:2015-08-09 09:00:39

标签: ios swift xcode6

希望每个人都能帮助我。我是新手。 如何将标签l1(从pickerview获取数据(数字))与文本字段txt相乘(获取用户输入(数字))。应使用按钮b开始计算。谢谢你的帮助

class Flächenbewehrung : UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

var pickerDataSource = ["Ø 6","Ø 8"]

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

@IBOutlet var l1: UILabel!

@IBAction func b(sender: AnyObject) {




}
override func viewDidLoad() {
    super.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 pickerDataSource.count
}

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

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if(row==0)
    {
        myLabel.text = "0,4"
    }
    else if(row==1)
    {
        myLabel.text = "0,6"
    }
}

2 个答案:

答案 0 :(得分:0)

我只是将数字与标签分开存储在不同的属性中。例如,您可以这样做:

class Flächenbewehrung : UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

var pickerDataSource = ["Ø 6","Ø 8"]

@IBOutlet weak var myPicker: UIPickerView!
@IBOutlet weak var myLabel: UILabel!
@IBOutlet var txt: UITextField!
var txtNumber: Double = 0.0 // Update this when you update txt
@IBOutlet var l1: UILabel!
var myLabelNumber: Double = 0.0 // Update this when you update l1
@IBAction func b(sender: AnyObject) {

  let result = txtNumber * myLabelNumber
  println(result) // Just for debug
  // store result somewhere or show it in your interface somehow

}

// ETC ....

}

答案 1 :(得分:0)

我还不确定你的意思。

您可以在pickerView:didSelectRow:

中进行数学运算
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  var multiplicator : Double = 1.0
  if (row == 0)
  {
    myLabel.text = "0,4"
    multiplicator = 0.4
  }
  else if (row == 1)
  {
    myLabel.text = "0,6"
    multiplicator = 0.6
  }

  let result = multiplicator *  (txt.text! as NSString).doubleValue
  // If you want to display the result in a label use
  // anyLabel.text = "\(result)"
}

代码不包含任何错误处理,例如标签txt中的文本值可转换为Double