0

I've been searching for this answer but can't seem to find it. I have a sectionHeader above a collection view. I added a button into the header but the function doesn't run. Mind taking a quick look for 1min and helping me out?

import UIKit
class SearchHeader: UICollectionViewCell {

    let filterLbl: UILabel = {
        let label = UILabel()
        label.text = "Filter"
        label.font = UIFont.systemFont(ofSize: 18)
        return label
    }()

    let addFilterBtn: UIButton = {
        let button = UIButton(type: .system)
        button.setImage(#imageLiteral(resourceName: "addFilter").withRenderingMode(.alwaysOriginal), for: .normal)
        button.addTarget(self, action: #selector(handleAddFilter), for: .touchUpInside)
        return button

    }()

    func handleAddFilter(button: UIButton) {
        print(1234)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .white

        addSubview(filterLbl)
        addSubview(addFilterBtn)

        filterLbl.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: nil, paddingTop: 10, paddingLeft: 10, paddingBottom: 10, paddingRight: 0, width: 0, height: 0)
        addFilterBtn.anchor(top: topAnchor, left: filterLbl.rightAnchor, bottom: bottomAnchor, right: nil, paddingTop: 10, paddingLeft: 5, paddingBottom: 10, paddingRight: 0, width: 0, height: 0)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
roshan_nazareth
  • 311
  • 5
  • 16
Kenny Ho
  • 409
  • 3
  • 16

1 Answers1

0

I found the answer.

lazy var addFilterBtn: UIButton = {
    let button = UIButton(type: .system)
    button.setImage(#imageLiteral(resourceName: "addFilter").withRenderingMode(.alwaysOriginal), for: .normal)
    button.addTarget(self, action: #selector(handleAddFilter), for: .touchUpInside)
    return button

}()

for some reason you have to change let into lazy var

Kenny Ho
  • 409
  • 3
  • 16