【发布时间】:2020-09-28 23:31:58
【问题描述】:
我使用的是版本 12.0.1 (12A7300)。我在 UIViews 上有一些按钮,在 UITableViewCells 中有一些按钮。表格单元格内按钮的操作位于我使用协议访问的父视图控制器中。在模拟器或我的设备上运行应用程序时。所有按钮都是以编程方式添加的。按钮不起作用。我重新安装了 Xcode 版本 11.7 (11E801a),一切正常。
有什么办法可以解决这个问题?这是一个错误还是发生了一些变化而我不知道?
这是我的代码示例。在 UITableViewCell 中:
var addToCartDelegate: AddToCartDelegate?
let addToCartButton: UIButton = {
let button = UIButton()
return button
}()
let applePayButton: PKPaymentButton = {
let button = PKPaymentButton(paymentButtonType: .buy, paymentButtonStyle: .whiteOutline)
return button
}()
func configureAddToCartButton() {
let button = addToCartButton
button.setTitle("Add To Cart", for: .normal)
button.titleLabel?.font = getScaledFont(forFont: Fonts.bold, textStyle: .body)
button.setTitleColor(Colors.primaryButtonTintColor, for: .normal)
button.tintColor = Colors.primaryButtonTintColor
button.backgroundColor = Colors.primaryButtonBackgroundColor
button.layer.cornerRadius = Attributes.cornerRadius
button.addTarget(self, action: #selector(addToCartButtonWasTapped), for: .touchUpInside)
}
func configureStackView() {
var stackView = UIStackView()
if useApplePay {
stackView = UIStackView(arrangedSubviews: [addToCartButton, applePayButton])
} else {
stackView = UIStackView(arrangedSubviews: [addToCartButton])
}
stackView.axis = .horizontal
stackView.spacing = 16
stackView.distribution = .fillEqually
addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
stackView.heightAnchor.constraint(equalToConstant: 44).isActive = true
stackView.rightAnchor.constraint(equalTo: rightAnchor, constant: -16).isActive = true
stackView.leftAnchor.constraint(equalTo: leftAnchor, constant: 16).isActive = true
}
@objc func addToCartButtonWasTapped() {
addToCartDelegate?.addItemToCart()
}
@objc func applePayButtonWasTapped() {
addToCartDelegate?.buyWithApplePay()
}
【问题讨论】:
-
刚刚测试了 Xcode 12 的新副本。按钮仍然不起作用。使用 Xcode 11.7 时一切正常。我会等到 Xcode 12 的几个更新发布。
-
如何将按钮添加到表格单元格?
addSubview或contentView.addSubview? -
对于这个特定的按钮,我有一个表格视图标题
tableView.tableHeaderView = tableViewHeader,我使用 addSubView 添加按钮,如tableViewHeader.addSubview(editProfileButton)。我正在添加到 stackview 中的其他按钮,并且我再次使用 addSubView 将 StackView 添加到 tableviewcell。我没有想到使用 contentView 添加它们。我会试试看是否有效。