how to call presentViewController from within a UICollectionViewCell

calling this function from a UIViewController results in no problems, but calling it from a UICollectionViewCell raises a pre-compilation error

Function :

func didTapShare(sender: UIButton)
{
    let textToShare = "Swift is awesome!  Check out this website about it!"

    if let myWebsite = NSURL(string: "http://www.google.com/")
    {
        let objectsToShare = [textToShare, myWebsite]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

        activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList]

        activityVC.popoverPresentationController?.sourceView = sender
        self.presentViewController(activityVC, animated: true, completion: nil)
    }
}

Error :

your cell has no member presentViewController

what to do?


UITableViewCell should never handle any business logic. It should be implemented in a view controller. You should use a delegate:

UICollectionViewCell subclass:

protocol CustomCellDelegate: class {
    func sharePressed(cell: MyCell)
}

class CustomCell: UITableViewCell {
    var delegate: CustomCellDelegate?

    func didTapShare(sender: UIButton) {
        delegate?.sharePressed(cell: self)
    }
}

ViewController:

class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    //...

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CustomCell
        cell.delegate = self
        return cell
    }
}

extension TableViewController: CustomCellDelegate {
    func sharePressed(cell: CustomCell) {
        guard let index = tableView.indexPath(for: cell)?.row else { return }
        //fetch the dataSource object using index
    }
}

That's because presentViewController is a UIViewController method, UITableViewCell does not has a method called presentViewController.

what to do?

You can use Delegation pattern for handling accessing the of the button's action (as @alexburtnik answer), or -for saving some extra work- I suggest to handle the action of the cell's button in the viewController by recognizing it via tag for it.

Note: Swift 3 Code.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell

        cell.myButton?.tag = indexPath.row
        cell.myButton?.addTarget(self, action: #selector(), for: .touchUpInside)

        return cell
    }

    func namesIsTapped(tappedButton: UIButton) {
        // get the user (from users array for example) by using the tag, for example:
        let currentUser = users[tappedButton.tag]

        // do whatever you want with this user now...

    }

Hope that helped.