views:

70

answers:

2

I have a UIButton and a UIView. The View sits above the button, and is larger than the button in size. The view itself is what I want to have accept the touch events, and I'd like to forward them to the button, so that all the normal button visual changes on touch happen.

I can't seem to make this work-- my UIView implements touchesBegan/Moved/Ended/Cancelled, and turns around and calls the same methods on the button with the same arguments. But the button doesn't respond.

I have ensured that the touch methods are in fact being called on the UIView.

Is there something obvious I'm missing here, or a better way of getting the control messages across? Or is there a good reason why this shouldn't work?

Thanks!

[Note: I'm not looking for workarounds for the view+button design. Let's assume that that's a requirement. I'm interested in the notion of controls that are touch proxies for other controls. Thanks.]

A: 

Are you sure your methods are being called? If you haven't set userInteractionEnabled=YES, then it won't work.

I've used such touch-forwarding before without problems, though, so I don't know why you're seeing the problems you're seeing.

tc.
Yes, the methods are being called as expected. Did you forward any other messages besides the touches* ones? Was the target of the forwarding a UIButton? Or something else? Thanks.
quixoto
It wasn't a UIButton; it was for "long touch" recognition I think. But I've written it at least twice, so I might be forgetting something.
tc.
+1  A: 

Create a ContainerView that contains a button and override hitTest:withEvent: so that it returns the UIButton instance.

falconcreek
Brilliant, thanks.
quixoto