我添加了两个let getMoney = (amount, limits) => {
let recur = (amount, nominals) => {
if (amount == 0) return {}; // success
if (!nominals.length) return; // failure
let nominal = nominals[0];
let count = Math.min(limits[nominal], Math.floor(amount / nominal));
for (let i = count; i >= 0; i--) {
let result = recur(amount - i*nominal, nominals.slice(1));
if (result) return i ? { [nominal]: i, ...result } : result;
}
}
return recur(amount, Object.keys(limits).map(Number).sort((a,b) => b - a));
};
// count of nominals in ATM
let limits = { 1000: 5, 500: 2, 100: 5, 50: 100, 30: 6 }
console.log(getMoney(1000, limits)); // {1000: 1}
console.log(getMoney(230, limits)); // {30: 1, 100: 2}
console.log(getMoney(200, limits)); // {100: 2}
console.log(getMoney(150, limits)); // {50: 1, 100: 1}
console.log(getMoney(120, limits)); // {30: 4}
,分别名为buttonA和buttonB,分别为UIButton
。当我点击SubView
时,将按预期调用buttonA
函数,但是当我点击handleButtonA
时,将同时调用buttonB
和handleButtonA
。无法理解如何解决此问题。这是代码。
handleButtonB
这太奇怪了,当我这样编写configUI函数时,效果很好。
class ViewController: UIViewController {
lazy var buttonA: UIButton = {
let btn = UIButton(type: .system)
btn.setTitle("buttonA", for: .normal)
btn.setTitleColor(UIColor.white, for: .normal)
btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
btn.backgroundColor = .green
btn.addTarget(self, action: #selector(handleButtonA), for: .touchUpInside)
return btn
}()
lazy var buttonB: UIButton = {
let btn = UIButton(type: .system)
btn.backgroundColor = .red
btn.setTitle("buttonB", for: .normal)
btn.setTitleColor(UIColor.white, for: .normal)
btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
btn.addTarget(self, action: #selector(handleButtonB), for: .touchUpInside)
return btn
}()
@objc func handleButtonA() {
print("buttonA tapped")
}
@objc func handleButtonB() {
print("buttonB tapped")
}
override func viewDidLoad() {
super.viewDidLoad()
configUI()
}
func configUI() {
view.backgroundColor = .white
view.addSubview(buttonA)
buttonA.snp.makeConstraints {
$0.left.bottom.equalToSuperview()
$0.height.width.equalTo(80)
}
view.addSubview(buttonB)
buttonB.snp.makeConstraints {
$0.right.bottom.equalToSuperview()
$0.height.width.equalTo(80)
}
}
}