简体   繁体   中英

How to use collection view buttons to select view controllers in swift

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

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

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.

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

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