我收到此错误:
类型'DiscountVC'的值没有成员'calculateTotal'。而且我不知道为什么。基本上,我正在尝试制作此计算器:
只要在discountTF
上插入任何值,它就应该起作用。此外,我还有一些预先打折的按钮,它们仅用于编辑折扣值。 subtotalLabel
值来自另一个ViewController
。出于测试目的,我使用的初始值为999.9。
import UIKit
class DiscountVC: UIViewController {
@IBOutlet var numericKeyboardView: UIView!
@IBOutlet var subtotalLabel: UILabel!
@IBOutlet var discountTF: UITextField!
@IBOutlet var totalLabel: UILabel!
var subtotal : Double = 999.9
var discount : Double = 0.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
addKeyboard(view: numericKeyboardView)
subtotal = 999.9
discount = 0.0
discountTF.addTarget(self, action: #selector(self.calculateTotal(_:)), for: UIControl.Event.editingChanged)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
func calculateTotal() {
let totalDouble = Double(subtotal) - Double(discountTF.text!)!
totalLabel.text = String(totalDouble)
}
func addKeyboard(view: UIView) {
let numericKeyboard = KeyboardVC(nibName: "NumericKeyboardVC", bundle: nil)
view.addSubview(numericKeyboard.view)
addChild(numericKeyboard)
}
@IBAction func fivePercentedButtonPressed(_ sender: Any) {
discount = Double(discountTF.text!)! * 0.05
discountTF.text = "\(discount)"
print(discount)
}
@IBAction func tenPercentButtonPressed(_ sender: Any) {
discount = Double(discountTF.text!)! * 0.1
discountTF.text = "\(discount)"
print(discount)
}
@IBAction func fifteenPercentButtonPressed(_ sender: Any) {
discount = Double(discountTF.text!)! * 0.15
discountTF.text = "\(discount)"
print(discount)
}
@IBAction func twentyPercentButtonPressed(_ sender: Any) {
discount = Double(discountTF.text!)! * 0.2
discountTF.text = "\(discount)"
print(discount)
}
@IBAction func goButton(_ sender: Any) {
}
}
答案 0 :(得分:3)
更改为
@objc func calculateTotal(_ tex:UITextField){ --- }