首页 > 解决方案 > View Controller 找不到 func 成员

问题描述

我收到此错误:

“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) {
    }

}

标签: swift

解决方案


改成

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

推荐阅读