UICollectionViewHeader中的UIButton

UICollectionViewHeader中的UIButton

编辑:代码在单元格的正文中工作,但不在标题中!

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

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/

10-08 20:52