Access Parent UICiewController UICollectionView

My question is pretty simple. I have a UIViewController containing a UICollectionView. When initializing my cells, I add a gesture recognizer to each of them, so that when I press and hold it, it will call a function using the selector.

This function then creates the UIAlertController that I want to introduce. (Basically, you hold the cell, it asks you if you want to delete it, and if you say yes, it excludes it from the CollectionView).

The problem is that I cannot imagine the UIAlertController from my UICollectionView, because it is not a ViewController.

I want to programmatically get a UIViewController that contains a UICollectionView in order to present this warning from a function that is inside the implementation of the UICollectionView.

+4
source share
2 answers

I do this by creating a protocol in my user account UICollectionViewCelland delegating these events back to UIViewController, something like this

AT MyCollectionViewCell

protocol MyCollectionViewCellDelegate: class {
    func didLongPressCell()
}

class MyCollectionViewCell:UICollectionViewCell {

    weak var delegate:MyCollectionViewCellDelegate?

    func longPressAction() {
        if let del = self.delegate {
            del.didLongPressCell
        }
    }

}

Then go back to MyViewController

class MyViewController:UIViewController, MyCollectionViewCellDelegate {

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyCollectionViewCell
        cell.delegate = self
        return cell
    }

    func didLongPressCell() {
        // do what you want with the event from the cell here
    }

}

Important bits to remember is the assignment of a delegate to each cell.

cell.delegate = self

And accept your new protocol in the view controller that you want to receive in

class MyViewController:UIViewController, MyCollectionViewCellDelegate

I have not tested this code, and I'm not sure of the best practice of storing links to the viewController in each cell like this, but I am doing something very similar, let me know how you do it.


:, UICollectionView, , .

MyViewController

class MyViewController:UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let collectionView = MyCollectionView()
        collectionView.viewController = self
        self.view.addSubview(collectionView)
    }

}

MyCollectionView

class MyCollectionView:UICollectionView, MyCollectionViewCellDelegate {

    weak var viewController:UIViewController?

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyCollectionViewCell
        cell.delegate = self
        return cell
    }

    func didLongPressCell() {
        if let vc = self.viewController {
            // make use of the reference to the view controller here
        }
    }

}

UICollectionViewCell ,

+4

rootViewController UIWindow

guard let delegate = UIApplication.shared.delegate? else {
     return
}
delegate.window.rootViewController.present(yourAlertController, animated:true)
0

Source: https://habr.com/ru/post/1667376/


All Articles