简体   繁体   中英

How to give xib cell 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 like this in HomeVC

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

note : if i use same collectionview cell then if i 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

You have to use closure.

cell class add this property

var connectionButtonAction: (() -> Void)?

and on the button action make this

@objc func connected(sender: UIButton){
    connectionButtonAction?()
  }

so finally for on the cell creation you have add this:

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

 
cell.connectionButtonAction = { [weak self] in
   print("cell button pressed")
}
return cell
}

I think this is the simple way, but also you get the same approach using delegates.

If you want to have the collection view cell's button trigger an action in the view controller that own's the collection view, you need to add the view controller as the target. You can do that in your cellForItemAtIndexPath, but you will need to remove the previous target/action so you don't keep adding a new target/action each time you reuse a cell:

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
    // Remove the previous target/action, if any
    cell.btnDetails.removeTarget(nil, action: nil,for: .allEvents)
    // Add a new target/action pointing to the method `productDetailsMain(_:)`
    cell.btnDetails.addTarget(self, action: #selector(productDetailsMain(_:)), for: .touchUpInside)
    return cell
}

You could also set up your cells to hold a closure as in luffy_064's answer.

Note that if you are running on iOS 14 or later, you can use the new addAction(_:for:) method of UIControl to add a UIAction to your button. (UIActions include a closure.)

That might look like this:

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
    // Remove the previous target/action, if any
        let identifier = UIAction.Identifier("button")

    removeAction(identifiedBy: identifier, for: .allEvents)
    // Add a new UIAction to the button for this cell
    let action = UIAction(title: "", image: nil, identifier: identifier, discoverabilityTitle: nil, attributes: [], state: .on) { (action) in
            print("You tapped button \(indexPath.row)")
            // Your action code here
        }
    cell(action, for: .touchUpInside)

    return cell
}

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