如何使用步进器增加标签值?
How to increase label value with stepper?
我有 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
}
}
使用选择器/IBAction 没有区别
@objc func foodPieceChangeStepper() {
let res = menuPieceStepper.value * foods.price.first!
foodPrice.text = "\(res)"
}
那么你应该将步长值设置为
menuPieceStepper.stepValue = 1.0
并在步进动作中
menuPieceStepper.value * currentPiecePrice
我有 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
}
}
使用选择器/IBAction 没有区别
@objc func foodPieceChangeStepper() {
let res = menuPieceStepper.value * foods.price.first!
foodPrice.text = "\(res)"
}
那么你应该将步长值设置为
menuPieceStepper.stepValue = 1.0
并在步进动作中
menuPieceStepper.value * currentPiecePrice