swift - 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) {
}
}
解决方案
改成
@objc func calculateTotal(_ tex:UITextField){ --- }
推荐阅读
- git - Visual Studio 2019:如何将您的项目保存到 Azure DevOps?
- java - 即使指定了选择参数,强制 Hibernate 始终使用默认的无参数构造函数
- xslt-2.0 - 对每个组使用 xslt
- networking - WNDR3800 运行 Gargoyle 1.11 网络配置 VLAN ID 设置时出错
- c++ - 多线程与 C++ 中的类
- c# - MVC Kendo Grid转换ToDataSourceResult耗时长,如何加快
- jira - ServiceDesk 通知中的混乱
- git - Bitbucket 使用 git-LFS 的最大 repo 大小
- angular - 动态设置 mat-tab 的背景颜色
- tensorflow - 从不同数据集中随机添加样本以检索最终数据集