Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Lewis Edmonds
Lewis Edmonds
2,823 Points

Popover segue from uicollectionview cell Swift

I am trying to instigate a popover segue from a button within a collection view cell. However, I can't set up an ordinary segue because I get an error about anchoring.

As I try to set this up programatically, I'm not sure whether I need to write the code within the collection view cell class or within the view controller (that the collection view sits in).

At the minute, I have my code within the collection view cell, as this is where the button is, but when I click it, nothing happens.

Code:

class LocationItemCell: UICollectionViewCell, UIPopoverPresentationControllerDelegate {

@IBOutlet weak var iconView: UIImageView!
@IBOutlet weak var iconLabel: UILabel!
@IBAction func infoButton(_ sender: Any) {
    let popController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popoverController") as UIViewController

    popController.modalPresentationStyle = UIModalPresentationStyle.popover


    popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
    popController.popoverPresentationController?.delegate = self
    popController.popoverPresentationController?.sourceView = sender as? UIView // button
    popController.popoverPresentationController?.sourceRect = (sender as AnyObject).bounds
}

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.none
} 

}