gpt4 book ai didi

swift - View Controller 找不到 func 成员

转载 作者:行者123 更新时间:2023-11-28 10:36:22 25 4
gpt4 key购买 nike

我收到这个错误:

Value of type 'DiscountVC' has no member 'calculateTotal'. And I have no clue why. Basically, I'm trying to make this calculator:

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

}

最佳答案

更改为

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

关于swift - View Controller 找不到 func 成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53420161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com