View Controller找不到函数成员

时间:2018-11-21 20:40:50

标签: swift

我收到此错误:

  

类型'DiscountVC'的值没有成员'calculateTotal'。而且我不知道为什么。基本上,我正在尝试制作此计算器:

enter image description here

只要在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) {
    }

}

1 个答案:

答案 0 :(得分:3)

更改为

@objc func calculateTotal(_ tex:UITextField){ --- }