{"id":77,"date":"2009-07-10T04:42:53","date_gmt":"2009-07-10T04:42:53","guid":{"rendered":"http:\/\/www.blogs.abeazam.com\/dev\/?p=77"},"modified":"2009-07-10T04:42:53","modified_gmt":"2009-07-10T04:42:53","slug":"extending-uibutton-in-objective-c","status":"publish","type":"post","link":"http:\/\/www.blogs.abeazam.com\/dev\/2009\/07\/extending-uibutton-in-objective-c\/","title":{"rendered":"Extending(Subclassing) UIButton in Objective-C"},"content":{"rendered":"<p>After playing around with UIButton class for a while I found Apple have been very sneaky with some \u00a0of their UI classes.<\/p>\n<p>What it appears they have done is use the UIButton class as a sort of wrapper for a number of private button classes. &#8220;UIRoundedRectButton&#8221; is one of those classes.<\/p>\n<p>&#8220;UIRoundedRectButton&#8221; is a private class so it can not be extended(subclassed). The initilizers normaly return an instance of self and if that self is not the class type trying to hold it there will be trouble. The problem is if you initialise UIButton with buttonWithType the class it returns is a private class. If you try and extended the UIButton once your custom class is initialized it returns a &#8220;UIRoundedRectButton&#8221; object which is private so you can not contain it.<\/p>\n<p>If you try the code below which uses the description you can see the object description is not UIButton.<\/p>\n<p>[c]UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];<br \/>\nNSLog(@&quot;myButton type: %@&quot;, [myButton description]);[\/c]<\/p>\n<p>In short you can not extend UIButton and initialize it with buttonWithType\u00a0 in Objective C. On the other hand you can extened UIButton for a button in interface builder. If\u00a0 you use interface builder you need to use initWithCoder as the initialiser. While interface builder does the private stuff.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After playing around with UIButton class for a while I found Apple have been very sneaky with some \u00a0of their UI classes. What it appears they have done is use the UIButton class as a sort of wrapper for a number of private button classes. &#8220;UIRoundedRectButton&#8221; is one of those classes. &#8220;UIRoundedRectButton&#8221; is a private [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[24,25,26,30,36,37,53],"tags":[],"_links":{"self":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts\/77"}],"collection":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":0,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}