{"id":1646,"date":"2013-01-29T20:49:31","date_gmt":"2013-01-29T16:49:31","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=1646"},"modified":"2013-01-29T20:52:50","modified_gmt":"2013-01-29T16:52:50","slug":"subclassing-uibutton-composition-over-inheritance-example","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=1646","title":{"rendered":"Subclassing UIButton (Composition over inheritance example)"},"content":{"rendered":"<p>I have created a custom class, preferring composition over inheritance and it works perfect. My custom class has a button and it knows it&#8217;s MCContact object. Also it draws a proper button and calculates frames automatically using MCContact object, that is passed.<\/p>\n<p><strong>Header file sample:<\/strong><\/p>\n<pre>\r\n#import <UIKit\/UIKit.h>\r\n\r\n@protocol MCContactViewDelegate;\r\n\r\n@interface MCContactView : UIView\r\n{\r\n\r\n}\r\n\r\n@property (nonatomic, strong) MCContact *mcContact;\r\n@property (nonatomic, weak) id &lt;MCContactViewDelegate&gt; delegate;\r\n\r\n- (id)initWithContact:(MCContact*)mcContact delegate:(id &lt;MCContactViewDelegate&gt;)delegate;\r\n\r\n@end\r\n\r\n@protocol MCContactViewDelegate <NSObject>\r\n\r\n- (void)contactViewButtonClicked:(MCContactView*)contactView;\r\n\r\n@end\r\n<\/pre>\n<p><strong>Implementation file:<\/strong><\/p>\n<pre>\r\n#import \"MCContactView.h\"\r\n\r\n@interface MCContactView()\r\n{\r\n    UIButton *_button;\r\n}\r\n\r\n@end\r\n\r\n@implementation MCContactView\r\n\r\n- (id)initWithContact:(MCContact*)mcContact delegate:(id &lt;MCContactViewDelegate&gt;)delegate\r\n{\r\n    self = [super initWithFrame:CGRectZero];\r\n    \r\n    if (self) {\r\n        \r\n        GetTheme();\r\n        \r\n        _mcContact = mcContact;\r\n        _delegate = delegate;\r\n        _button = [UIButton buttonWithType:UIButtonTypeCustom];\r\n        \r\n        UIImage *normalBackgroundImage = [[UIImage imageNamed:@\"tokenNormal.png\"] stretchableImageWithLeftCapWidth:12.5 topCapHeight:12.5];\r\n        [_button setBackgroundImage:normalBackgroundImage forState:UIControlStateNormal];\r\n        \r\n        UIImage *highlightedBackgroundImage = [[UIImage imageNamed:@\"tokenHighlighted.png\"] stretchableImageWithLeftCapWidth:12.5 topCapHeight:12.5];\r\n        [_button setBackgroundImage:highlightedBackgroundImage forState:UIControlStateHighlighted];\r\n        \r\n        _button.titleLabel.font = [theme contactButtonFont];\r\n        [_button setTitleColor:[theme contactButtonTextColor] forState:UIControlStateNormal];\r\n        \r\n        [_button setTitleEdgeInsets:UIEdgeInsetsMake(4, 6, 4, 6)];\r\n        \r\n        NSString *tokenString = ([allTrim(mcContact.name) length]>0) ? mcContact.name : mcContact.eMail;\r\n        [_button setTitle:tokenString forState:UIControlStateNormal];\r\n        \r\n        [_button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];\r\n        \r\n        CGSize size = [tokenString sizeWithFont:[theme contactButtonFont]];\r\n        size.width += 20;\r\n        if (size.width > 200) {\r\n            size.width = 200;\r\n        }\r\n        size.height = normalBackgroundImage.size.height;\r\n        [_button setFrame:CGRectMake(0, 0, size.width, size.height)];\r\n        \r\n        self.frame = _button.frame;\r\n        [self addSubview:_button];\r\n    }\r\n    \r\n    return self;\r\n}\r\n\r\n\r\n- (void)buttonClicked:(id)sender\r\n{\r\n    [self.delegate contactViewButtonClicked:self];\r\n}\r\n\r\n@end\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have created a custom class, preferring composition over inheritance and it works perfect. My custom class has a button and it knows it&#8217;s MCContact object. Also it draws a proper button and calculates frames automatically using MCContact object, that is passed. Header file sample: #import @protocol MCContactViewDelegate; @interface MCContactView : UIView { } @property [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1646","post","type-post","status-publish","format-standard","hentry","category-ios"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Subclassing UIButton (Composition over inheritance example) - Denis Kutlubaev<\/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:\/\/blog.alwawee.ru\/?p=1646\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Subclassing UIButton (Composition over inheritance example) - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"I have created a custom class, preferring composition over inheritance and it works perfect. My custom class has a button and it knows it&#8217;s MCContact object. Also it draws a proper button and calculates frames automatically using MCContact object, that is passed. Header file sample: #import @protocol MCContactViewDelegate; @interface MCContactView : UIView { } @property [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=1646\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2013-01-29T16:49:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-01-29T16:52:50+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.alwawee.com\/?attachment_id=1982\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1646#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1646\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"Subclassing UIButton (Composition over inheritance example)\",\"datePublished\":\"2013-01-29T16:49:31+00:00\",\"dateModified\":\"2013-01-29T16:52:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1646\"},\"wordCount\":54,\"commentCount\":0,\"articleSection\":[\"iOS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1646#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1646\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1646\",\"name\":\"Subclassing UIButton (Composition over inheritance example) - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"datePublished\":\"2013-01-29T16:49:31+00:00\",\"dateModified\":\"2013-01-29T16:52:50+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1646#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1646\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1646#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Subclassing UIButton (Composition over inheritance example)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Subclassing UIButton (Composition over inheritance example) - Denis Kutlubaev","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:\/\/blog.alwawee.ru\/?p=1646","og_locale":"en_US","og_type":"article","og_title":"Subclassing UIButton (Composition over inheritance example) - Denis Kutlubaev","og_description":"I have created a custom class, preferring composition over inheritance and it works perfect. My custom class has a button and it knows it&#8217;s MCContact object. Also it draws a proper button and calculates frames automatically using MCContact object, that is passed. Header file sample: #import @protocol MCContactViewDelegate; @interface MCContactView : UIView { } @property [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=1646","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2013-01-29T16:49:31+00:00","article_modified_time":"2013-01-29T16:52:50+00:00","og_image":[{"url":"http:\/\/blog.alwawee.com\/?attachment_id=1982","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=1646#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=1646"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"Subclassing UIButton (Composition over inheritance example)","datePublished":"2013-01-29T16:49:31+00:00","dateModified":"2013-01-29T16:52:50+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=1646"},"wordCount":54,"commentCount":0,"articleSection":["iOS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=1646#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=1646","url":"https:\/\/blog.alwawee.ru\/?p=1646","name":"Subclassing UIButton (Composition over inheritance example) - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"datePublished":"2013-01-29T16:49:31+00:00","dateModified":"2013-01-29T16:52:50+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=1646#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=1646"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=1646#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"Subclassing UIButton (Composition over inheritance example)"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/1646","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1646"}],"version-history":[{"count":0,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/1646\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}