{"id":121,"date":"2019-05-06T23:29:32","date_gmt":"2019-05-07T04:29:32","guid":{"rendered":"https:\/\/mikespsyche.com\/?p=121"},"modified":"2019-05-07T19:58:26","modified_gmt":"2019-05-08T00:58:26","slug":"embedding-view-controllers","status":"publish","type":"post","link":"https:\/\/mikespsyche.com\/embedding-view-controllers\/","title":{"rendered":"Embedding View Controllers"},"content":{"rendered":"<h3>Intro<\/h3>\n<p>Good code delegates responsibility to the objects responsible for that code. It just makes it easier for others to understand and reason about, especially in large projects.<\/p>\n<p>However, a common practice is to place a <code>UITableView<\/code> as a view within a <code>UIViewController<\/code> and then set the <code>delegate<\/code> and <code>dataSource<\/code> of the table view to the view controller. In some situations, this is perfectly fine. However, if you want to segregate your code with a logical grouping, it&#8217;d make sense to have a <code>UITableViewController<\/code> manager a table view and a <code>UIViewController<\/code> manage a regular view. The catch is, how do you combine them when you want to have both on the same screen?<\/p>\n<p>The answer is embedding! It&#8217;s actually quite simple.<\/p>\n<h3>Breakdown<\/h3>\n<ol>\n<li>In this scenario, you have a <code>UIViewController<\/code> and <code>UITableViewController<\/code>\n<ul>\n<li>(by no means is this exclusive to this exact combo, but it is a common scenario)<\/li>\n<li><img decoding=\"async\" src=\"\/wp-content\/uploads\/screenshots\/Main_storyboard_\u2014_Edited_228134F5.png\" alt=\"embed start\" \/><\/li>\n<\/ul>\n<\/li>\n<li>Place a <em>Container View<\/em> in the view controller you&#8217;d like to make the parent&#8230;\n<ol>\n<li><img decoding=\"async\" src=\"\/wp-content\/uploads\/screenshots\/Pasted_Image_5_6_19__10_40_PM_228135AA.png\" alt=\"find the container view\" \/><\/li>\n<li>Delete the additional View Controller it creates<\/li>\n<li><img decoding=\"async\" src=\"\/wp-content\/uploads\/screenshots\/Main_storyboard_\u2014_Edited_228136EB.png\" alt=\"drag it onto your canvas\" \/>\n<ul>\n<li>notice that it states that it&#8217;s simply a <code>UIView<\/code>. Under the hood, this <em>is<\/em> the case, but <em>Interface Builder<\/em> won&#8217;t work unless we use an explicit <em>Container View<\/em> :\/<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<\/li>\n<li>Finally, <code>{right-click}<\/code>-drag or <code>{control}<\/code>-drag from the container view to the view controller you&#8217;d like to embed within. When you let go, choose <em>embed<\/em>\n<ul>\n<li>\n<video src=\"\/wp-content\/uploads\/screen_recordings\/embed_container_view.mov\" autoplay loop><\/video>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>That&#8217;s it! Or, it can be if you don&#8217;t care about passing data back and forth. If you do, continue on!<\/p>\n<h3>Segue Setup<\/h3>\n<p>To get the two View Controllers to talk to each other, it won&#8217;t take too much work, but before we do that, you need to set an identifier on the segue between the two controllers:<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/screenshots\/Main_storyboard_\u2014_Edited_22813AAD.png\" alt=\"segue identifier\" \/><br \/>\nYou can set the identifer to anything you wish, just as long it&#8217;s unique amongst segue identifiers&#8230; But just be sure to name it something logical.<\/p>\n<p>Here&#8217;s the code:<\/p>\n<pre><code class=\"language-swift\">class ViewController: UIViewController {\n    var embeddedTableViewController: EmbeddedTableViewController? { \/\/ you remembered to create and set the class for your table view controller, right?!\n        didSet {\n            embeddedTableViewController?.muhDatas = [&quot;Yo dawg, I heard you like embedding view controllers, so I embedded a view controller in your view controller so you can control views while you control views.&quot;]\n        }\n    }\n\n    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {\n        if segue.identifier == &quot;EmbedTableController&quot; {\n            guard let dest = segue.destination as? EmbeddedTableViewController else { return }\n            embeddedTableViewController = dest\n            \/\/ now I can access the embedded controller and give it data or make it respond to button presses or whatnot\n        }\n    }\n}<\/code><\/pre>\n<p>In this code, the following happens:<\/p>\n<ol>\n<li>We create a variable to hold a reference to the embedded view controller (you remembered to subclass it, right?!)\n<ol>\n<li>We add a <code>didSet<\/code> observer to this reference to immediately provide it with data after being set.<\/li>\n<\/ol>\n<\/li>\n<li>The <code>prepare(for segue)<\/code> function is used to create the link between the two controllers.<\/li>\n<li>That&#8217;s really about it&#8230; Everything beyond this is an implementation detail. Set up data passing in a reasonable, logical way. <\/li>\n<\/ol>\n<h3>Cheat your way to success<\/h3>\n<p>If you&#8217;d like a copy of the code, it can be found on the <a href=\"https:\/\/github.com\/mredig\/BlogDemo\/tree\/embed-viewcontroller\">embed-viewcontroller<\/a> branch of my BlogDemo repo.<\/p>\n<p>Now to make an InceptionController&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Intro Good code delegates responsibility to the objects responsible for that code. It just makes it easier for others to understand and reason about, especially in large projects. However, a<a class=\"read-more more-link\" href=\"https:\/\/mikespsyche.com\/embedding-view-controllers\/\">Continue reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,10],"tags":[],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-swift","category-xcode"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Embedding View Controllers | Mike&#039;s Psyche<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/mikespsyche.com\/embedding-view-controllers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Embedding View Controllers | Mike&#039;s Psyche\" \/>\n<meta property=\"og:description\" content=\"Intro Good code delegates responsibility to the objects responsible for that code. It just makes it easier for others to understand and reason about, especially in large projects. However, aContinue reading\" \/>\n<meta property=\"og:url\" content=\"https:\/\/mikespsyche.com\/embedding-view-controllers\/\" \/>\n<meta property=\"og:site_name\" content=\"Mike&#039;s Psyche\" \/>\n<meta property=\"article:published_time\" content=\"2019-05-07T04:29:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-05-08T00:58:26+00:00\" \/>\n<meta name=\"author\" content=\"Michael\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/mikespsyche.com\/embedding-view-controllers\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/mikespsyche.com\/embedding-view-controllers\/\"},\"author\":{\"name\":\"Michael\",\"@id\":\"https:\/\/mikespsyche.com\/#\/schema\/person\/6ea249535952c80ed7d586291ebd3046\"},\"headline\":\"Embedding View Controllers\",\"datePublished\":\"2019-05-07T04:29:32+00:00\",\"dateModified\":\"2019-05-08T00:58:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/mikespsyche.com\/embedding-view-controllers\/\"},\"wordCount\":403,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/mikespsyche.com\/#\/schema\/person\/6ea249535952c80ed7d586291ebd3046\"},\"articleSection\":[\"Swift\",\"Xcode\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/mikespsyche.com\/embedding-view-controllers\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/mikespsyche.com\/embedding-view-controllers\/\",\"url\":\"https:\/\/mikespsyche.com\/embedding-view-controllers\/\",\"name\":\"Embedding View Controllers | Mike&#039;s Psyche\",\"isPartOf\":{\"@id\":\"https:\/\/mikespsyche.com\/#website\"},\"datePublished\":\"2019-05-07T04:29:32+00:00\",\"dateModified\":\"2019-05-08T00:58:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/mikespsyche.com\/embedding-view-controllers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/mikespsyche.com\/embedding-view-controllers\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/mikespsyche.com\/embedding-view-controllers\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/mikespsyche.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Embedding View Controllers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/mikespsyche.com\/#website\",\"url\":\"https:\/\/mikespsyche.com\/\",\"name\":\"Mike's Psyche\",\"description\":\"Swift and maybe more?\",\"publisher\":{\"@id\":\"https:\/\/mikespsyche.com\/#\/schema\/person\/6ea249535952c80ed7d586291ebd3046\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/mikespsyche.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/mikespsyche.com\/#\/schema\/person\/6ea249535952c80ed7d586291ebd3046\",\"name\":\"Michael\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/mikespsyche.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e754bec0598d19c61efb5820591b4ab3?s=96&d=wavatar&r=r\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e754bec0598d19c61efb5820591b4ab3?s=96&d=wavatar&r=r\",\"caption\":\"Michael\"},\"logo\":{\"@id\":\"https:\/\/mikespsyche.com\/#\/schema\/person\/image\/\"},\"description\":\"I love Swift, World of Warcraft, my wife, and my dog. (not necessarily in that order)\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Embedding View Controllers | Mike&#039;s Psyche","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/mikespsyche.com\/embedding-view-controllers\/","og_locale":"en_US","og_type":"article","og_title":"Embedding View Controllers | Mike&#039;s Psyche","og_description":"Intro Good code delegates responsibility to the objects responsible for that code. It just makes it easier for others to understand and reason about, especially in large projects. However, aContinue reading","og_url":"https:\/\/mikespsyche.com\/embedding-view-controllers\/","og_site_name":"Mike&#039;s Psyche","article_published_time":"2019-05-07T04:29:32+00:00","article_modified_time":"2019-05-08T00:58:26+00:00","author":"Michael","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Michael","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mikespsyche.com\/embedding-view-controllers\/#article","isPartOf":{"@id":"https:\/\/mikespsyche.com\/embedding-view-controllers\/"},"author":{"name":"Michael","@id":"https:\/\/mikespsyche.com\/#\/schema\/person\/6ea249535952c80ed7d586291ebd3046"},"headline":"Embedding View Controllers","datePublished":"2019-05-07T04:29:32+00:00","dateModified":"2019-05-08T00:58:26+00:00","mainEntityOfPage":{"@id":"https:\/\/mikespsyche.com\/embedding-view-controllers\/"},"wordCount":403,"commentCount":0,"publisher":{"@id":"https:\/\/mikespsyche.com\/#\/schema\/person\/6ea249535952c80ed7d586291ebd3046"},"articleSection":["Swift","Xcode"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/mikespsyche.com\/embedding-view-controllers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/mikespsyche.com\/embedding-view-controllers\/","url":"https:\/\/mikespsyche.com\/embedding-view-controllers\/","name":"Embedding View Controllers | Mike&#039;s Psyche","isPartOf":{"@id":"https:\/\/mikespsyche.com\/#website"},"datePublished":"2019-05-07T04:29:32+00:00","dateModified":"2019-05-08T00:58:26+00:00","breadcrumb":{"@id":"https:\/\/mikespsyche.com\/embedding-view-controllers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/mikespsyche.com\/embedding-view-controllers\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/mikespsyche.com\/embedding-view-controllers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/mikespsyche.com\/"},{"@type":"ListItem","position":2,"name":"Embedding View Controllers"}]},{"@type":"WebSite","@id":"https:\/\/mikespsyche.com\/#website","url":"https:\/\/mikespsyche.com\/","name":"Mike's Psyche","description":"Swift and maybe more?","publisher":{"@id":"https:\/\/mikespsyche.com\/#\/schema\/person\/6ea249535952c80ed7d586291ebd3046"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/mikespsyche.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/mikespsyche.com\/#\/schema\/person\/6ea249535952c80ed7d586291ebd3046","name":"Michael","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/mikespsyche.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e754bec0598d19c61efb5820591b4ab3?s=96&d=wavatar&r=r","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e754bec0598d19c61efb5820591b4ab3?s=96&d=wavatar&r=r","caption":"Michael"},"logo":{"@id":"https:\/\/mikespsyche.com\/#\/schema\/person\/image\/"},"description":"I love Swift, World of Warcraft, my wife, and my dog. (not necessarily in that order)"}]}},"_links":{"self":[{"href":"https:\/\/mikespsyche.com\/wp-json\/wp\/v2\/posts\/121"}],"collection":[{"href":"https:\/\/mikespsyche.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mikespsyche.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mikespsyche.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mikespsyche.com\/wp-json\/wp\/v2\/comments?post=121"}],"version-history":[{"count":3,"href":"https:\/\/mikespsyche.com\/wp-json\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":140,"href":"https:\/\/mikespsyche.com\/wp-json\/wp\/v2\/posts\/121\/revisions\/140"}],"wp:attachment":[{"href":"https:\/\/mikespsyche.com\/wp-json\/wp\/v2\/media?parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mikespsyche.com\/wp-json\/wp\/v2\/categories?post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mikespsyche.com\/wp-json\/wp\/v2\/tags?post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}