Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add properties/methods to UIButton
    primarykey
    data
    text
    <p>I need to add two additional properties (NSString * and NSMutableArray *) along with three extra methods to UIButton. I also want to reference the new objects using a supertype if that is possible. I do not necessarily want to subclass (as I read that it is tricky and not recommended), but I am quite new to Objective-C and iOS development and don't know what else to do.</p> <p>I tried to subclass UIButton with my subclass implementing a formal protocol in the following way:</p> <pre><code>@interface Button : UIButton &lt;MyProtocol&gt; ... </code></pre> <p>However, I found out this doesn't work like I thought it would, as buttonWithType: returns an object from a subclass. What else can I do to achieve the desired result?</p> <p>-- EDIT: Ok, my current code is like this:</p> <pre><code>@interface Button : UIButton &lt;SteapeObject&gt; { ActionQueue * actions; Meta meta; } @property (nonatomic, retain) ActionQueue * actions; @property (nonatomic) Meta meta; - (id) initWithFrame:(CGRect)frame; ... </code></pre> <p>And the implementation:</p> <pre><code>- (id) initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { NSLog (@"finally"); } return self; } </code></pre> <p>An still doesn't work. It seems that when I invoke:</p> <pre><code>Button * button = [Button buttonWithType: UIButtonTypeRoundedRect]; NSLog (@"%@", [button description]); </code></pre> <p>I should get two 'finally' strings and two descriptions in the log. However, I only get the two description strings:</p> <pre><code>[Session started at 2011-02-24 09:47:14 +0100.] 2011-02-24 09:47:15.431 IphoneClient3[702:207] &lt;UIRoundedRectButton: 0x5f47690; frame = (0 0; 0 0); opaque = NO; layer = &lt;CALayer: 0x5f47240&gt;&gt; 2011-02-24 09:47:15.461 IphoneClient3[702:207] &lt;UIRoundedRectButton: 0x6a0f000; frame = (0 0; 0 0); opaque = NO; layer = &lt;CALayer: 0x6a344b0&gt;&gt; </code></pre> <p>And you can see that the type is still UIRoundedRectButton, but the buttons do not respond to my added methods. Actually, since my overriden initWithFrame doesn't get called, that is to be expected. Perhaps I should default to implementing a custom control...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload