MNM
MNM

Reputation: 2743

swift Turn a generated imageView into a UIButton in code

I am trying to add a tap gesture to a generated Image and it works ok but keeps crashing the program once I click on the image. What did I do wrong here. Any help is appreciated.

let tapGesture = UITapGestureRecognizer(target: self, action: "image Tapped")
            let imageView = UIImageView(image: UIImage(data: data));
            imageView.addGestureRecognizer(tapGesture)
            imageView.userInteractionEnabled = true

This is the error I get

2016-07-12 09:19:30.241 XXX[2170:27661] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ‘-[MyViewController image Tapped]: unrecognized selector sent to instance 0x7fe811e30560'
 *** First throw call stack:
 (
0   CoreFoundation                      0x000000010e2a7d85 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000011144ddeb objc_exception_throw + 48
2   CoreFoundation                      0x000000010e2b0d3d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x000000010e1f6cfa ___forwarding___ + 970
4   CoreFoundation                      0x000000010e1f68a8 _CF_forwarding_prep_0 + 120
5   UIKit                               0x0000000110391b28 _UIGestureRecognizerSendTargetActions + 153
6   UIKit                               0x000000011038e19a _UIGestureRecognizerSendActions + 162
7   UIKit                               0x000000011038c197 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 843
8   UIKit                               0x0000000110394655 ___UIGestureRecognizerUpdate_block_invoke898 + 79
9   UIKit                               0x00000001103944f3 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342
10  UIKit                               0x0000000110381e75 _UIGestureRecognizerUpdate + 2634
11  UIKit                               0x000000010ff0e48e -[UIWindow _sendGesturesForEvent:] + 1137
12  UIKit                               0x000000010ff0f6c4 -[UIWindow sendEvent:] + 849
13  UIKit                               0x000000010febadc6 -[UIApplication sendEvent:] + 263
14  UIKit                               0x000000010fe94553 _UIApplicationHandleEventQueue + 6660
15  CoreFoundation                      0x000000010e1cd301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
16  CoreFoundation                      0x000000010e1c322c __CFRunLoopDoSources0 + 556
17  CoreFoundation                      0x000000010e1c26e3 __CFRunLoopRun + 867
18  CoreFoundation                      0x000000010e1c20f8 CFRunLoopRunSpecific + 488
19  GraphicsServices                    0x0000000112d68ad2 GSEventRunModal + 161
20  UIKit                               0x000000010fe99f09 UIApplicationMain + 171
21  ApivitaClient                       0x000000010d6a1482 main + 114
22  libdyld.dylib                       0x0000000111f1192d start + 1
23  ???                                 0x0000000000000001 0x0 + 1

) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

Upvotes: 0

Views: 62

Answers (2)

Igor
Igor

Reputation: 12303

replace

let tapGesture = UITapGestureRecognizer(target: self, action: "image Tapped")

with

let tapGesture = UITapGestureRecognizer(target:self, action: #selector(imageTapped))

and create func imageTapped() {}

Upvotes: 2

Alonso Urbano
Alonso Urbano

Reputation: 2264

The action part in the UITapGestureRecognizer initializer means which method will be executed. And the action part means who's responsible for that action, who's the delegate.

Use this to initialize the gesture recognizer:

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("imageTapped:"))

Please note the colon : after the name of the method (imageTapped). This means the gesture recognizer will call the imageTapped method and pass the sender parameter, which it will be useful if you want to determine which gesture recognizer was triggered. This is really useful if you have several images that can be tapped, so you don't have to build a method for each one.

Then you need to declare the imageTapped method:

func imageTapped(sender: UIGestureRecognizer) {
    print("Image tapped")
}

You can declare the function to work without parameter as well, just declare the function without the sender parameter and delete the colon in the action selector. Anyway, I recommend to keep it, it may be useful somewhere and it's better if you learn it that way.

Upvotes: 1

Related Questions