简体   繁体   中英

How to add xib cell's button action in HomeVC in swift

I have created xib collectionview cell.. and i am able to use all its values in HomeVC like below

class HomeVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

@IBOutlet var collectionView: UICollectionView!

 override func viewDidLoad() {

super.viewDidLoad()
let nib = UINib(nibName: "MainCollectionViewCell", bundle: nil)
collectionView.registerNib(nib, forCellWithReuseIdentifier: "MainCollectionViewCell")

 }

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MainCollectionViewCell", forIndexPath: indexPath) as! MainCollectionViewCell

 cell.arrivingLabel.text = indexData.arriv
 cell.lblDiscountedPrice.text = indexData.discPrice

 
return cell
}

like below i can give action to xib cell button, but i want xib cell button action in HomeVC class how, please guide me here

   cell.btnDetails.addTarget(self, action: #selector(connected(sender:)), for: .touchUpInside)

   @objc func connected(sender: UIButton){
  }

i want xib cell's button action in HomeVC

 @IBAction func productDetailsMain(_ sender: UIButton){
  }

note : if i use same collectionview cell then i can drag from HomeVC button action outlet to collectionview cell button then its adding.. but if i use xib cell in collectionview then this process is not working.. how to give xib cell button action in HomeVC class

Screenshot: not able to connect action outlet in HomeVC

在此处输入图片说明

class HomeVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

@IBOutlet var collectionView: UICollectionView!

 override func viewDidLoad() {

super.viewDidLoad()
let nib = UINib(nibName: "MainCollectionViewCell", bundle: nil)
collectionView.registerNib(nib, forCellWithReuseIdentifier: "MainCollectionViewCell")

 }

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MainCollectionViewCell", forIndexPath: indexPath) as! MainCollectionViewCell
 cell.btnDetails.tag = indexPath.row// it will identify the tapped button cell index
cell.btnDetails.addTarget(self, action: #selector(connected(sender:)), for: .touchUpInside)
 cell.arrivingLabel.text = indexData.arriv
 cell.lblDiscountedPrice.text = indexData.discPrice

 
return cell
}

@objc func connected(sender: UIButton){
    let tag = sender.tag
    print(tag)

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM