如何收集勒贝尔文本中的数字

时间:2017-04-18 14:35:03

标签: swift

我想收集价格以标签打印总金额

@IBOutlet weak var typlabale: UILabel!
@IBOutlet weak var fruitLabel: UILabel!

@IBOutlet weak var typPrice: UILabel!
@IBOutlet weak var fruitPrice: UILabel!    

@IBOutlet weak var totaleValue: UILabel!


 var CupValue: Int = 8
 var ConeValue: Int = 20
 var StrawberryValue : Int = 20
 var RaspberryValue : Int = 20
 var BananaValue : Int = 10

// Mark : - Typ selec 
@IBAction func typesegm(_ sender: Any) {
    switch typesegm.selectedSegmentIndex
    {
    case 0:
        typlabale.text = "Cup"
        typPrice.text = "$\(CupValue)"

    case 1:
        typlabale.text = "Cone"
        typPrice.text = "$\(ConeValue)"

    default:
        break; 
    }
}
// Fruit
@IBAction func fruitSegm(_ sender: Any) {
    switch fruit.selectedSegmentIndex
    {
    case 0:
        fruitLabel.text = "Strawberry"
        fruitPrice.text = "$\(StrawberryValue)"
    case 1:
        fruitLabel.text = "Raspberry"
        fruitPrice.text = "$\(RaspberryValue)"
    case 2:
        fruitLabel.text = "Banana"
        fruitPrice.text = "$\(BananaValue)"
    default:
        break;
    }
}

1 个答案:

答案 0 :(得分:0)

添加此行

totaleValue.text = "$\(Int(typPrice.text!.replacingOccurrences(of: "$", with: ""))! + Int(fruitPrice.text!.replacingOccurrences(of: "$", with: ""))!)"

在此typesegmfruitSegm方法的末尾。

@IBAction func typesegm(_ sender: Any) {
    switch typesegm.selectedSegmentIndex
    { ... }
    totaleValue.text = "$\(Int(typPrice.text!.replacingOccurrences(of: "$", with: ""))! + Int(fruitPrice.text!.replacingOccurrences(of: "$", with: ""))!)"

}

@IBAction func fruitSegm(_ sender: Any) {
    switch fruit.selectedSegmentIndex
    { ... }
    totaleValue.text = "$\(Int(typPrice.text!.replacingOccurrences(of: "$", with: ""))! + Int(fruitPrice.text!.replacingOccurrences(of: "$", with: ""))!)"

}