IBAction wont connect to my element

78 views Asked by At

Ive been trying to search online and in my "book" to found a conclusive answer, but haven't been able to. I am working with an UIImageView and want to connect it to an IBAction. When Ctrl-dragging to my correlating VC.m, it won't let me. I then tried to go under attributes inspector and tick the "User Interaction Enabled" checkbox, AND under the identity inspector I checked traits checkbox for Button. Shouldn't this make my element/control be able to connect to an action? Digging a little further I was reading about passive, active and static controls, but I got the notion that you should be able to connect an action to any control the user interacts with, since they all inherit from UIControl.

Any enlightenment very much appreciated.

(I also tried with a few other elements, but I seem to be able able to connect the action working with a button)

3

There are 3 answers

3
Paulw11 On BEST ANSWER

A UIImageView does not inherit from UIControl. If you want to perform an action when the user touches your image then you will need to add a UIGestureRecognizer to it.

3
Yaser On

First a couple of clarifications:

  • Traits is used by accessibility only. It does not provide you with touch-detection
  • User interaction enabled also does not mean what you think it means documentation

I think you need some sort of UIGestureRecognizer or an UIButton

0
wildBillMunson On

It sounds like you are trying to get a UIImageView to act like a UIButton. Why don't you simply replace the UIImageView with a UIButton and set the button's background image to the same image you were using for the imageView? Then hook the button up to the IBAction. A button is an active control - it is designed to do exactly what you want, so why not use it?