Re: How to subclass UIButton?
Re: How to subclass UIButton?
- Subject: Re: How to subclass UIButton?
- From: WT <email@hidden>
- Date: Thu, 8 Apr 2010 08:42:46 +0200
> If you want to use the existing buttonWithStyle: method, all you need
> to do is implement initWithFrame:, since that's the default
> initializer
Is the fact that -initWithFrame: is the designated initializer documented anywhere? The docs for UIButton don't mention it. If this is documented and/or won't change, then it's the solution I've been looking for.
Thanks!
On Apr 8, 2010, at 8:14 AM, Jack Nutting wrote:
> I've successfully subclassed UIButton with no problems. I my case I
> wasn't interested in the "style" since I was implementing my own, so I
> created my own class method which just calls initWithFrame:
>
> + (id)buttonWithFrame:(CGRect)frame {
> return [[[self alloc] initWithFrame:frame] autorelease];
> }
>
> - (id)initWithFrame:(CGRect)frame {
> if (self = [super initWithFrame:frame]) {
> // do my additional initialization here
> }
> return self;
> }
>
> Note that this way the buttonType isn't explicitly set to anything,
> which probably means that it's UIButtonTypeCustom. The Docs don't
> seem to actually specify that, but since that's the 0 value in the
> enum, that's likely what happens (and that seems to be the observable
> behavior as well)
>
> If you want to use the existing buttonWithStyle: method, all you need
> to do is implement initWithFrame:, since that's the default
> initializer, then call [MyButton buttonWithStyle:whatever] and you're
> done!
>
> If you also want to be able to use your subclass in .xib files, then
> you'll also need to implement initWithCoder: (calling the super
> implementation) and put your initialization code in there as well.
>
> --
> // jack
> // http://nuthole.com
> // http://learncocoa.org
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden