[swift] Install PopoverPresentationController & Reflect Tap Information to MainViewContoller

Tried to install PopoverPresentationController which let small window pop-up

Poped up Window is CollectionViewController

and let CollectionViewController notice Tap information to MainViewController when CollectionViewController Cell tapped

Installed Code is below.

Create delegate on CollectionViewController, then setup this delegate on MainViewContoller

IMG_6864

MainViewController

import UIKit

class ViewController: UIViewController,UIPopoverPresentationControllerDelegate,TestCollectionViewControllerDelegate {
    
    @IBOutlet var button:UIButton!
    @IBOutlet var testImage:UIImageView!
    var testViewController:TestCollectionViewController!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    //on StoryBorad, should setup segue from Button to TestCollectionViewController(ID : popup)
    override func prepare(for segue:UIStoryboardSegue,
                          sender:Any!) {
        if let identifier = segue.identifier {
            switch identifier {
            case "popup":
                if let contentVC = segue.destination as? TestCollectionViewController, let button = sender as? UIButton {
                    
                    //set delegate for tapped
                    contentVC.delegate = self
                    
                    //popoverPresentationController
                    let controller = contentVC.popoverPresentationController
                    controller?.delegate = self

                    //background color (mainly arrow)
                    controller?.backgroundColor = UIColor.gray

                    //direction of arrow
                    controller?.permittedArrowDirections = .down

                                        //the object pointed by arrow#1
                    // controller?.sourceView = button //un-necessary?

                                        //the object pointed by arrow#2
                    controller?.sourceRect = button.bounds
                    
                    //display size
                    contentVC.preferredContentSize=CGSize(width: 400, height: 200)
                    
                }
            default: break
            }
        }
    }
    
    // for iPhone
    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return .none
    }

    // Run Delegate
    func didChangeBackgroundImage(str:String) {
        testImage.image = UIImage(named: "stamp_" + String(str) + ".png")
    }
    
}

CollectionViewContorller

import UIKit

protocol TestCollectionViewControllerDelegate: class {
    func didChangeBackgroundImage(str:String)
}

class TestCollectionViewController: UICollectionViewController {
    
    @IBOutlet var myCollectionView : UICollectionView!
    weak var delegate: TestCollectionViewControllerDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        myCollectionView.delegate = self
        myCollectionView.dataSource = self
    }
    
    // cell number
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 40
    }
    
    // cell setting
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TestCell", for: indexPath as IndexPath)
        let imageView = cell.contentView.viewWithTag(1) as! UIImageView
        imageView.image = UIImage(named: "stamp_" + String(num) + ".png")
        
        return cell
    }
    
    // method for tapped
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        self.delegate?.didChangeBackgroundImage(str: String(num))
    }
    
    
    
}