Managing events on a custom UIControl

Beav picture Beav · Mar 5, 2011 · Viewed 12.6k times · Source

I am subclassing UIControl to compose a custom control that contains different standard controls.

For this discussion let's assume that my custom UIControl contains a UIButton only.

What I would like to achieve is that clicking anywhere in the custom UIControl generates a click event for that custom UIControl. The standard behavior is that the UIButton will process and consume (i.e. not forward) the click event.

As subclassing UIButton is discouraged, I can't really find a straightforward way of achieving this.

Any suggestions?

Answer

Beav picture Beav · Mar 6, 2011

I came up with a simple solution that doesn't need subclassing of the UIButton.

In the action method defined for the UIButton's TouchUpInside control event, I have added the following line of code:

[self sendActionsForControlEvents:UIControlEventTouchUpInside];

This results in the TouchUpInside control event being called, when clicking anywhere in the custom UIControl.