{"id":38,"date":"2010-05-02T18:11:56","date_gmt":"2010-05-03T02:11:56","guid":{"rendered":"http:\/\/bynomial.com\/blog\/?p=38"},"modified":"2010-05-03T15:27:31","modified_gmt":"2010-05-03T23:27:31","slug":"simplified-popovers","status":"publish","type":"post","link":"http:\/\/bynomial.com\/blog\/?p=38","title":{"rendered":"Simplified popovers"},"content":{"rendered":"<p>In this post, I&#8217;ll present a microclass that can give you simplified popover (<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">UIPopoverController<\/div><\/div>\n<p>) handling in many cases, including automatic rotation updates. <em>Links to code below.<\/em><\/p>\n<h2>What PopoverHelper does<\/h2>\n<p>Normally, this is how you need to work with a popover:<\/p>\n<ol>\n<li>Set up the content view controller that will appear in the popover.<\/li>\n<li>Alloc and init the popover.<\/li>\n<li>Often, you need to resize the popover content.<\/li>\n<li>Gather information about where and how the popover will be presented.<\/li>\n<li>Present the popover.<\/li>\n<li>Track orientation changes, and update the popover&#8217;s position accordingly.<\/li>\n<li>Dismiss the popover according to UI actions, often using a popover delegate.<\/li>\n<li>Release the popover controller.<\/li>\n<\/ol>\n<p>In my use so far, I&#8217;ve found that I&#8217;m repeating myself when it comes to many of these steps. \u00c2\u00a0So, like any decent coder, I feel the compelling urge to refactor. \u00c2\u00a0The result is<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">PopoverHelper<\/div><\/div>\n<p>, a compact class that can turn the above steps into code like this:<\/p>\n<pre>MyViewController *myController = [MyViewController beInit];\r\nUIPopoverController *popover = \u00c2\u00a0[PopoverHelper popoverForViewController:myController];\r\n[popover presentPopoverFromView:myView];<\/pre>\n<p>Behind the scenes, it sets up the popover object as an autonomous object with a delegate that releases it upon dismissal, sets the content size to match that of<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">myController<\/div><\/div>\n<p>, and presents the popover from<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">myView<\/div><\/div>\n<p>&#8216;s frame within its superview. \u00c2\u00a0It also listens for device orientation changes, and re-presents your popover for you from the same source view, or dismisses the popover if the source view becomes hidden. \u00c2\u00a0You can programmatically dismiss the popover with a call to<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">[myPopover dismissAndRelease]<\/div><\/div>\n<p>, but you never have to explicitly worry about its memory management unless you take over the delegate.<\/p>\n<h2>How it works<\/h2>\n<p>The code is simple and speaks for itself <em>(links to actual files below)<\/em>. \u00c2\u00a0Here&#8217;s the header:<\/p>\n<p><a href=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/05\/PopoverHelper_h2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-43\" title=\"PopoverHelper_h\" src=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/05\/PopoverHelper_h2.png\" alt=\"\" width=\"600\" height=\"280\" srcset=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/05\/PopoverHelper_h2.png 600w, http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/05\/PopoverHelper_h2-300x140.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>In <a href=\"http:\/\/bynomial.com\/blogfiles\/PopoverHelper.m\">the m file<\/a>, you can see that PopoverHelper works by setting the delegate for you to its own handler, and tracks the source view and last device orientation. \u00c2\u00a0When a rotation occurs, it re-presents the popover for you. \u00c2\u00a0When the popover is discarded, it cleans up and is ready to give you a new popover.<\/p>\n<h2>Source and References<\/h2>\n<p>The beInit method is explained in <a href=\"http:\/\/bynomial.com\/blog\/?p=33\">an earlier post<\/a>.<\/p>\n<p><a href=\"http:\/\/bynomial.com\/blogfiles\/PopoverHelper.h\">PopoverHelper.h<\/a><\/p>\n<p><a href=\"http:\/\/bynomial.com\/blogfiles\/PopoverHelper.m\">PopoverHelper.m<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, I&#8217;ll present a microclass that can give you simplified popover ( UIPopoverController ) handling in many cases, including automatic rotation updates. Links to code below. What PopoverHelper does Normally, this is how you need to work with a popover: Set up the content view controller that will appear in the popover. Alloc [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[47,40,15],"tags":[48,49],"_links":{"self":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/38"}],"collection":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=38"}],"version-history":[{"count":0,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/38\/revisions"}],"wp:attachment":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}