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 07:59:38 +0200
On Apr 8, 2010, at 6:44 AM, john chen wrote:
> I think UIButton is not designed for subclassing.
That was my hunch as well.
> You can try
> composition, or create UIButton category method for custom-build
> button. Apple has sample code in UICatalog to create custom-build
> button.
A category on UIButton won't work for me because I need to add extra state to the custom button. The best alternative I've come up with is to have a custom UIView object that contains a UIButton instance and which hijacks touch events by overriding -hitTest:withEvent: to return self. Once the swiping and reordering (as in the Autoscroll sample code) have been dealt with, this "proxy" UIView instance can decide if/when to forward a tap gesture to the button. So, yes, composition seems to be the way to go here.
> On Wed, Apr 7, 2010 at 2:26 PM, WT wrote:
>> Hello all,
>>
>> I need to create a custom bar of UIButton instances which can be scrolled left and right using a swipe gesture. I am thus following the "Autoscroll" sample code from Apple, which subclasses UIImageView to produce a scrollable bar of thumbnail images. My problem is how to correctly subclass UIButton, since the only available means of instantiating a button is through the class method +buttonWithType:.
>>
>> Once I allocate an instance of the subclass, how do I initialize it with the desired type? The buttonType property is read-only and there is no instance method -initWithType: in the public API.
>>
>> One alternative I see is to use composition rather than subclassing, essentially creating a decorator for UIButton, but I'd rather not have to do that.
>>
>> Another option is to fake a button using UIImageView, just like the "Autoscroll" example does, but I'd prefer to have actual UIButton instances.
>>
>> Am I missing something trivial here? If UIButton was designed in such a way to prevent subclassing, what would you suggest as the best alternative?
>>
>> Thanks in advance.
>> WT.
_______________________________________________
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