4

I currently have a UIControl, which has a number of subviews (image, label).

Unfortunately when I use addTarget etc. It doesn't detect touches on the subviews.

  [myCustomView addTarget:self action:@selector(touchedView:)
             forControlEvents:UIControlEventTouchUpInside];

Is it possible for the UIControl to detect touches on subviews or should i be approaching it differently.

Jack Thompson
  • 41
  • 1
  • 2

3 Answers3

23

Just note that all subviews of a UIControl which shouldn't receive touch events themselves must have userInteractionEnabled set to NO and not YES (which is the default).

So you must set this on all labels, images, etc.

I just ran into this and figured it out after a while :)

snod
  • 2,442
  • 3
  • 20
  • 22
  • Thank you so much, I was looking for a solution for while but never thought that it would end this way. – iwat Dec 14 '10 at 06:41
0

Your method call is correct.

myCustomView should be the image or label being added.

  • so i have to iterate over all the subviews and add target. that also means that they also all have to be UIControl subclasses – Jack Thompson May 06 '10 at 06:02
  • "so i have to iterate over all the subviews and add target." That is correct. I think they have to be UIView subclasses, which are super classes of UIControl. – just_another_coder May 06 '10 at 06:18
0
[myCustomView setUserInteractionEnabled:YES];
Manjunath
  • 4,545
  • 2
  • 26
  • 31