如何使用步进器增加标签价值?

时间:2019-03-07 11:02:11

标签: ios swift swift4 uistepper

我有priceLabel,它具有Food的属性price。我想用第一个值来增加这个priceLabel。例如,priceLabel的第一个值为15.00,并且当用户点击步进器时,第一个值随其自身的增加而增加。 (例如2点以增加15 + 15 = 30

我认为像stepper.stepValue = priceLabel.text

这是我的密码

import UIKit
class DetailViewController: UIViewController {

@IBOutlet weak var foodTitle: UILabel!
@IBOutlet weak var foodSubTitle: UILabel!
@IBOutlet weak var foodPiece: UILabel!
@IBOutlet weak var foodPrice: UILabel!
@IBOutlet weak var drinkPicker: UITextField!
@IBOutlet weak var constantPieceLabel: UILabel!

var menuPieceStepper : UIStepper!
var drinkPickerView = UIPickerView()

var selectDrinkType: [String] = []
var detailFoodName : [String] = []
var detailFoodPrice : [Double] = [0.0]

var constPiece = 0

let foods = Food(name: ["Hamburger big mac",
                           "Patates",
                           "Whopper",
                           "Steakhouse"], price: [15.0, 20.0, 25.0, 30.0])

我需要使用@IBAction#selector吗?

@IBAction func foodPieceStepper(_ sender: UIStepper) {
    foodPrice.text = Double(sender.value).description
}

@objc func foodPieceChangeStepper() {

    foodPrice.text = "\(menuPieceStepper.value)"
}

//TODO:- Add to basket
@IBAction func addBasket(_ sender: Any) {

        let destinationVC = MyCartViewController()

        destinationVC.fromDetailFoodNames = foods.name
        destinationVC.fromDetailFoodPrices = foods.price

    dismiss(animated: true)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if(segue.identifier == "addToCartSegue") {
        if let addToCartVC = segue.destination as? MyCartViewController {


            addToCartVC.fromDetailFoodNames = foods.name
            addToCartVC.fromDetailFoodPrices = foods.price

    }
   }
}

override func viewDidLoad() {
    super.viewDidLoad()

    menuPieceStepper = UIStepper()
    menuPieceStepper.value = 0.0
    menuPieceStepper.minimumValue = 0.0
    menuPieceStepper.maximumValue = 30.0
    menuPieceStepper.stepValue = foods.price.first!
    menuPieceStepper.addTarget(self, action: #selector(foodPieceChangeStepper), for: .valueChanged)

    foodPieceChangeStepper()

    drinkPickerView.delegate = self
    drinkPicker.inputView = drinkPickerView
    selectDrinkType = ["Ayran", "Kola", "Su", "Fanta", "Şalgam", "Sprite"]

    foodTitle.text = detailFoodName.description
    foodPrice.text = detailFoodPrice.description

    self.navigationController?.navigationItem.title = "Sipariş Detayı"

}

override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.navigationBar.isHidden = false

}

override func viewWillDisappear(_ animated: Bool) {
    self.navigationController?.navigationBar.isHidden = true
}

}

1 个答案:

答案 0 :(得分:0)

使用选择器/ IBAction没有区别

@objc func foodPieceChangeStepper() { 
  let res = menuPieceStepper.value * foods.price.first!
  foodPrice.text = "\(res)"
}

然后您应该将步长值设置为

menuPieceStepper.stepValue = 1.0

在步进动作中

menuPieceStepper.value * currentPiecePrice