{"id":1148,"date":"2017-10-06T09:46:35","date_gmt":"2017-10-06T00:46:35","guid":{"rendered":"http:\/\/blue-bear.jp\/kb\/?p=1148"},"modified":"2017-10-06T09:53:04","modified_gmt":"2017-10-06T00:53:04","slug":"swift-install-popoverpresentationcontroller-reflect-tap-information-to-mainviewcontoller","status":"publish","type":"post","link":"https:\/\/blue-bear.jp\/kb\/swift-install-popoverpresentationcontroller-reflect-tap-information-to-mainviewcontoller\/","title":{"rendered":"[swift] Install PopoverPresentationController &#038; Reflect Tap Information to MainViewContoller"},"content":{"rendered":"<p>Tried to install\u00a0PopoverPresentationController which let small window pop-up<\/p>\n<p>Poped up Window is\u00a0CollectionViewController<\/p>\n<p>and let\u00a0CollectionViewController notice Tap information to MainViewController when\u00a0CollectionViewController Cell tapped<\/p>\n<p>Installed Code is below.<\/p>\n<p>Create delegate on\u00a0CollectionViewController, then setup this delegate on MainViewContoller<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1141 size-large\" src=\"http:\/\/blue-bear.jp\/kb\/wp-content\/uploads\/2017\/10\/IMG_6864-576x1024.png\" alt=\"IMG_6864\" width=\"576\" height=\"1024\" srcset=\"https:\/\/blue-bear.jp\/kb\/wp-content\/uploads\/2017\/10\/IMG_6864-576x1024.png 576w, https:\/\/blue-bear.jp\/kb\/wp-content\/uploads\/2017\/10\/IMG_6864-169x300.png 169w, https:\/\/blue-bear.jp\/kb\/wp-content\/uploads\/2017\/10\/IMG_6864.png 750w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/p>\n<p>MainViewController<\/p>\n<pre class=\"\">import UIKit\r\n\r\nclass ViewController: UIViewController,UIPopoverPresentationControllerDelegate,TestCollectionViewControllerDelegate {\r\n    \r\n    @IBOutlet var button:UIButton!\r\n    @IBOutlet var testImage:UIImageView!\r\n    var testViewController:TestCollectionViewController!\r\n    \r\n    override func viewDidLoad() {\r\n        super.viewDidLoad()\r\n    }\r\n    \r\n    \/\/on StoryBorad, should setup segue from Button to TestCollectionViewController(ID : popup)\r\n    override func prepare(for segue:UIStoryboardSegue,\r\n                          sender:Any!) {\r\n        if let identifier = segue.identifier {\r\n            switch identifier {\r\n            case \"popup\":\r\n                if let contentVC = segue.destination as? TestCollectionViewController, let button = sender as? UIButton {\r\n                    \r\n                    \/\/set delegate for tapped\r\n                    contentVC.delegate = self\r\n                    \r\n                    \/\/popoverPresentationController\r\n                    let controller = contentVC.popoverPresentationController\r\n                    controller?.delegate = self\r\n\r\n                    \/\/background color (mainly arrow)\r\n                    controller?.backgroundColor = UIColor.gray\r\n\r\n                    \/\/direction of arrow\r\n                    controller?.permittedArrowDirections = .down\r\n\r\n\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\/\/the object pointed by arrow#1\r\n                    \/\/ controller?.sourceView = button \/\/un-necessary?\r\n\r\n\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\/\/the object pointed by arrow#2\r\n                    controller?.sourceRect = button.bounds\r\n                    \r\n                    \/\/display size\r\n                    contentVC.preferredContentSize=CGSize(width: 400, height: 200)\r\n                    \r\n                }\r\n            default: break\r\n            }\r\n        }\r\n    }\r\n    \r\n    \/\/ for iPhone\r\n    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -&gt; UIModalPresentationStyle {\r\n        return .none\r\n    }\r\n\r\n    \/\/ Run Delegate\r\n    func didChangeBackgroundImage(str:String) {\r\n        testImage.image = UIImage(named: \"stamp_\" + String(str) + \".png\")\r\n    }\r\n    \r\n}\r\n<\/pre>\n<p>CollectionViewContorller<\/p>\n<pre class=\"\">import UIKit\r\n\r\nprotocol TestCollectionViewControllerDelegate: class {\r\n    func didChangeBackgroundImage(str:String)\r\n}\r\n\r\nclass TestCollectionViewController: UICollectionViewController {\r\n    \r\n    @IBOutlet var myCollectionView : UICollectionView!\r\n    weak var delegate: TestCollectionViewControllerDelegate?\r\n    \r\n    override func viewDidLoad() {\r\n        super.viewDidLoad()\r\n        \r\n        myCollectionView.delegate = self\r\n        myCollectionView.dataSource = self\r\n    }\r\n    \r\n    \/\/ cell number\r\n    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {\r\n        return 40\r\n    }\r\n    \r\n    \/\/ cell setting\r\n    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {\r\n        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: \"TestCell\", for: indexPath as IndexPath)\r\n        let imageView = cell.contentView.viewWithTag(1) as! UIImageView\r\n        imageView.image = UIImage(named: \"stamp_\" + String(num) + \".png\")\r\n        \r\n        return cell\r\n    }\r\n    \r\n    \/\/ method for tapped\r\n    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {\r\n        \r\n        self.delegate?.didChangeBackgroundImage(str: String(num))\r\n    }\r\n    \r\n    \r\n    \r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Tried to install\u00a0Pop<\/p>\n","protected":false},"author":1,"featured_media":1141,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/1148"}],"collection":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/comments?post=1148"}],"version-history":[{"count":4,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/1148\/revisions"}],"predecessor-version":[{"id":1152,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/1148\/revisions\/1152"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media\/1141"}],"wp:attachment":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media?parent=1148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/categories?post=1148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/tags?post=1148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}