How can I programmatically link an UIView or UIImageView with an event like "touch up inside"?

Posted by Thanks on Stack Overflow See other posts from Stack Overflow or by Thanks
Published on 2009-05-26T12:00:04Z Indexed on 2010/05/02 17:47 UTC
Read the original article Hit count: 711

Filed under:
|
|

Interface Builder will only allow me to hook up such events for an button. But like in HTML, I just want to haven an blank UIImageView where - as soon as the user taps it - a method is invoked. I hope there is some cool programmatically way of doing that, which I don't know about.

UPDATE:

In my View Controller that creates the UIImageView I tried to do this:

SEL actionSelector = @selector(doSomethingWhenImageIsTouched:);
[self.myUIImageView addTarget:nil action:actionSelector forControlEvents:UIControlEventTouchUpInside];

The compiler gives me a warning, that UIImageView may not respond to addTarget:... what do I have to do so that it works with an UIImageView. I see in the docs that UIImageView does not inherit from UIControl, but addTarget: is part of UIControl.

UPDATE:

I ended up creating an UIButton after creating the UIImageView. Then I set the frame of that button to the frame of the UIImageView, and alpha to 0.1f. For some reason, it will not work if alpha is 0.0f. And then, I did that cool addTarget: thing...

© Stack Overflow or respective owner

Related posts about iphone

Related posts about event-handling