{"id":16,"date":"2009-10-10T19:09:25","date_gmt":"2009-10-11T00:09:25","guid":{"rendered":"http:\/\/blog.ddg.com\/?p=16"},"modified":"2009-10-10T19:09:26","modified_gmt":"2009-10-11T00:09:26","slug":"rhview-and-delegate-drawing","status":"publish","type":"post","link":"http:\/\/blog.ddg.com\/?p=16","title":{"rendered":"RHView and Delegate Drawing&#8230;"},"content":{"rendered":"<p>The delegate drawing pattern implemented by a <span style=\"font-family: monospace; font-style: normal\">RHView<\/span> is not a decorator pattern. It is a simple delegate pattern. I intend you, though, to use it somewhat differently than a traditional delegate. A traditional delegate would be responsible for drawing directly in the <span style=\"font-family: monospace; font-style: normal\">RHView<\/span>. And, of course, you can use it that way. I use it differently though.<\/p>\n<p>I create all of my drawing commands as methods of the <span style=\"font-family: monospace; font-style: normal\">RHView<\/span>. This allows complex graphics commands to be encapsulated in the view. The delegate becomes a drawing coordinator and does not a directly draw in the view. <\/p>\n<p>Here is a simple example &#8212; a subclass of <span style=\"font-family: monospace; font-style: normal\">RHView<\/span> to draw a graph:<\/p>\n<pre>\n#import \"RHView.h\"<br\/>\n@class Curve;<br\/>\n@interface GraphView : RHView {<br\/>\n@private\n    Curve *curve;\n}<br\/>\n@property (retain, nonatomic) Curve *curve;<br\/>\n- (void) drawBorder: (CGRect) rect inContext: (CGContextRef) context;\n- (void)  drawCurve: (CGRect) rect inContext: (CGContextRef) context;<br\/>\n@end\n<\/pre>\n<p>It has a single instance variable &#8212; a <span style=\"font-family: monospace; font-style: normal\">curve<\/span>. And it has two drawing methods &#8212; <span style=\"font-family: monospace; font-style: normal\">-drawBorder:inContext:<\/span> and <span style=\"font-family: monospace; font-style: normal\">-drawCurve:inContext:<\/span>. The delegate object only needs to implement the <span style=\"font-family: monospace; font-style: normal\">RHViewDelegate<\/span> protocol. <\/p>\n<p>Here is a sample delegate implementation:<\/p>\n<pre>\n- (void) drawView: (GraphView *) view inRect: (CGRect) rect inContext: (CGContextRef) context {<br\/>\n    [view  drawCurve: rect inContext: context];\n    [view drawBorder: rect inContext: context];<br\/>\n}\n<\/pre>\n<p>All drawing commands are encapsulated in the subclass of <span style=\"font-family: monospace; font-style: normal\">RHView<\/span>. Your view controller is, in my opinion, the natural delegate of the <span style=\"font-family: monospace; font-style: normal\">RHView<\/span>. You then just create new view controllers to coordinate the new behavior. It is totally congruent with the MVC pattern. <\/p>\n<p>This pattern really cleaned up my code. I hope it cleans yours up too.<\/p>\n<p>An aside: the <span style=\"font-family: monospace; font-style: normal\">RHViewDelegate<\/span> is the key mechanism needed to initiate a decorator drawing pattern.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The delegate drawing pattern implemented by a RHView is not a decorator pattern. It is a simple delegate pattern. I intend you, though, to use it somewhat differently than a traditional delegate. A traditional delegate would be responsible for drawing directly in the RHView. And, of course, you can use it that way. I use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-code","category-iphone"],"_links":{"self":[{"href":"http:\/\/blog.ddg.com\/index.php?rest_route=\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.ddg.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ddg.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ddg.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ddg.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16"}],"version-history":[{"count":1,"href":"http:\/\/blog.ddg.com\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":17,"href":"http:\/\/blog.ddg.com\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions\/17"}],"wp:attachment":[{"href":"http:\/\/blog.ddg.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ddg.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ddg.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}