2017-07-18 65 views
1

UIButton我在UICollectionViewCell中,但它不工作,當我點擊它去另一個ViewController鄙視我訪問它。我發佈我的兩個班級代碼。爲什麼它發生?不工作UIButton來自UICollectionViewCell

類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 
} 

}

類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) 

} 

}

+0

你需要爲你的創建委託,然後打開showAppDetailForApp從你的DetailController。 – GeneCode

+0

@GeneCode感謝您的回答,請嘗試更多詳細信息 –

+0

這段代碼中會發生什麼,您可以詳細說明一下,以及您想要做什麼? – KKRocks

回答

0

試試這個:

解決方案1 ​​

在視圖控制器

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 

} 

解決方案2

在細胞類

  btn.addTarget(self, action: #selector(self.giveReviewBtnTarget), for: .touchUpInside) 
+0

在cellItemAtindexPath中顯示錯誤 –

+0

在此處添加錯誤消息。 – KKRocks

+0

:類型'DetailController'的值沒有成員'giveReviewBtnTarget' –

相關問題