2

A UIButton i made in UICollectionViewCell, but it not works at all,when i click it to go another ViewController despise i access it. i post my two class code. why it is happening?

class DetailController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

var arrDetailProduct = [Product]()


let descriptionCellid = "descriptioncellid"
let reviewAverageRateCellid = "reviewaveragerateid"
let baceCellId = "baceCellid"



override func viewDidLoad() {
    super.viewDidLoad()


    collectionView?.register(ReviewAverageRate.self, forCellWithReuseIdentifier: reviewAverageRateCellid)
    collectionView?.register(BaseCellNew.self, forCellWithReuseIdentifier: baceCellId)
}

func showAppDetailForApp(){

    let appDetailController = GiveReviewViewController()

    navigationController?.pushViewController(appDetailController, animated: true)
}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reviewAverageRateCellid, for: indexPath) as! ReviewAverageRate
        cell.detailControllr = self
        return cell


}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}

}

class ReviewAverageRate: UICollectionViewCell {

var detailControllr = DetailController()
override init(frame: CGRect) {
    super.init(frame: frame)

    setupDigitalReviewAverageRate()
}

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


let giveReviewBtn: UIButton = {

    let btn = UIButton()
    btn.setTitle("GIVE REVIEW", for: .normal)

    btn.setTitleColor(.white, for: .normal)

    btn.backgroundColor = UIColor.orange
    btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 10)
    btn.addTarget(self, action: #selector(giveReviewBtnTarget), for: .touchUpInside)

    return btn
}()

func giveReviewBtnTarget() {

    detailControllr.showAppDetailForApp()

}

func setupDigitalReviewAverageRate() {

    addSubview(giveReviewBtn)

    addConstraintsWithFormat("H:|[v0(80)]|", views:  giveReviewBtn)

    addConstraintsWithFormat("V:|[v0(20)]|", views: giveReviewBtn)

}

}

Quiet Islet
  • 536
  • 1
  • 8
  • 28

2 Answers2

0

Try this :

Solution 1

In ViewController

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reviewAverageRateCellid, for: indexPath) as! ReviewAverageRate
        cell.detailControllr = self
            cell.giveReviewBtn.addTarget(self, action: #selector(self.giveReviewBtnTarget), for: .touchUpInside)
        return cell
}


func giveReviewBtnTarget() {
   // add your code 

}

Solution 2

In cell class

            btn.addTarget(self, action: #selector(self.giveReviewBtnTarget), for: .touchUpInside)
Community
  • 1
  • 1
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • : Value of type 'DetailController' has no member 'giveReviewBtnTarget' – Quiet Islet Jul 18 '17 at 06:09
  • you need to present your controller there . i have forgot to remove . – KKRocks Jul 18 '17 at 06:11
  • you need to use both code which i working it is your solution. – KKRocks Jul 18 '17 at 06:34
  • can you me help to answer this question. i want to detect one button in tableview cell by click on button. here is link https://stackoverflow.com/questions/48970198/how-to-detect-one-button-in-tableview-cell?noredirect=1#comment84948065_48970198 – Quiet Islet Feb 26 '18 at 03:34
-1

For any one who still struggle with this problem,

Just need to extend:  UICollectionViewDelegateFlowLayout

Example

class YourClassName: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

...

}
glennsl
  • 28,186
  • 12
  • 57
  • 75