TouchUpInside on UIView with children

Aleks G picture Aleks G · May 24, 2012 · Viewed 13.4k times · Source

I may be missing something here, but...

I have a UIView with a few children (a couple of UILabel's and UIImageView). I need to catch an event when the user clicks (taps) anywhere within the parent UIView - visible part of the UIView or any of its children).

What's the best way of doing this?

Answer

DarkByte picture DarkByte · May 24, 2012

You can change the UIView's class in the Identity Inspector ... make it UIControl and then you can add an event for UITouchUpInside for your view - to catch the taps on it.

Good luck!

EDIT: here's a step-by-step with screenshots:

The view... Original view - of type UIView

... of course, no events. UIView has no events you can use

Go back and change UIView with UIControl and... Change UIView to UIControl

Ta-daa! Events! UIControl allows you to use events

Hook a method to the UITouchUpInside event Add event for UITouchUpInside

This is how it should look like in the interface file The interface of the method

This is how it should look like in the implementation file (you can, of course, replace the logging with anything you want) The implementation of the method