gpt4 book ai didi

ios - UICollectionViewHeader 中的 UIButton 不起作用

转载 作者:行者123 更新时间:2023-11-29 05:57:45 24 4
gpt4 key购买 nike

编辑:代码在单元格正文中有效,但在标题中无效!

我已以编程方式向我的 View 添加了一个按钮。一切都正确显示。当我按下按钮时,它会突出显示。问题是该函数没有被调用。有人有办法解决这个问题吗?

let addPerson : UIButton = {
let btn = UIButton(type: .system)
btn.setTitle("Test button", for: .normal)
btn.setTitleColor(.white, for: .normal)
btn.backgroundColor = .darkGray
btn.layer.masksToBounds = true
btn.addTarget(self, action: #selector(handleAddPerson), for: .touchUpInside)
btn.translatesAutoresizingMaskIntoConstraints = false
return btn
}()

@objc func handleAddPerson() {
print("test")
}


//Following is inside the init
let stackView = UIStackView(arrangedSubviews: [addPerson,addGroup])
stackView.distribution = .fillEqually
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.spacing = 10
addSubview(stackView)

NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: topAnchor, constant: 8),
stackView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8),
stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8),
stackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8),
])

最佳答案

尝试将选择器更改为该配合:

@objc func handleAddPerson(_ sender: UIButton?) {
print("test")
}

关于ios - UICollectionViewHeader 中的 UIButton 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54984010/

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