0

I have a CollectionViewController with 3 buttons populated. The view works well, but how do I select different view controllers based on the button selected? I added the button as an action, but I don't know how to specify which button is selected so I can send the user to different viewcontrollers.

import UIKit

private let reuseIdentifier = "Cell"

class CollectionViewController: UICollectionViewController {

var imageArray = [UIImage(named: "tempOwl.png"), UIImage(named: "tempPuzzle.png"), UIImage(named: "tempHouse.png")]

override func viewDidLoad() {
    super.viewDidLoad()

    self.clearsSelectionOnViewWillAppear = false
    self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

}

@IBAction func menuButton(sender: UIButton) {
    let controller = storyboard?.instantiateViewControllerWithIdentifier("myHome")
    presentViewController(controller!, animated: true, completion: nil)

}

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

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

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

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell
    let imageView = cell.viewWithTag(1) as! UIButton
    imageView.setBackgroundImage(self.imageArray[indexPath.row], forState: .Normal)

    return cell
}
}

3 Answers 3

2

The button action method for your button in cell

func buttonAction(sender : UIButton) {

   var selectedButtonCell = sender.superview as! UICollectionViewCell
   //Incase your button is inside cell.contentview
   // var selectedButtonCell = sender.superview.superview as! UICollectionviewCell
   var indexPath = collectionView.indexPathForCell(selectedButtonCell)
   if indexPath.row == 0 {
      //Button in first cell is selected
      //Send user to first button view controller 
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

This looks like a good solution, but I keep getting the error: Use of undeclared type 'UICollectionviewCell'
Typo mistake. Change it to UICollectionViewCell. V caps
Worked great! Thank you!
0

I suggest to create a class of type UICollectionViewCell and create the IBAction in the cell.

And set the collectionViewController as delegate. So you can pass values.

So you can create a function in the collection view controller and give it a param like a int 1 for cell 1.

Comments

0

If you have multiple buttons in the cell you should implement a delegate. Then, set your view controller as a delegate of the cell. Sample implementation of such a delegate could look like:

protocol YourCustomCellDelegate {

    func firstButtonPressed(cell: YourCustomCell)
    func secondButtonPressed(cell: YourCustomCell)
}

class YourCustomCell : UICollectionViewCell {

    var delegate:YourCustomCellDelegate?

    @IBOutlet weak var firstButton: UIButton!
    @IBOutlet weak var secondButton: UIButton!

    @IBAction func firstButtonTapped(sender: AnyObject) {

        delegate?.firstButtonPressed(self)
    }

    @IBAction func secondButtonTapped(sender: AnyObject) {

        delegate?.secondButtonPressed(self)
    }
}

1 Comment

I should have specified, I only have one button in each cell, but I don't know how to differentiate between the buttons pushed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.