Search code examples
iosswiftuilabelglobaluitapgesturerecognizer

Custom tap recognizer method won’t work if implemented in other class


Is is possible create a global method accesible by any other class to add tap recognizers?

(I'm a beginner in swift)

Context/What I want to achieve:

Since I will be using a lot of tap recognizers (for labels and imageViews) in various views, I want to create a default method that helps me save a few lines of code everytime I need a tap recognizer.

-> Example of what I want

class Toolbox {
 static func customTapRecognizerForLabel () {}
 static func customTapRecognizerForImage () {}
}

So I can use them in different view controllers:

class ViewControllerOne{
 Toolbox.customTapRecognizerForLabel()
}
class ViewControllerTwo{
 Toolbox.customTapRecognizerForLabel()
}

What have I done so far?:

What didn't worked: I created the Toolbox class in Toolbox.swift, tried to call it in other view controllers but it does not work. (Also tried to define a shared instance of the class instead of using static methods but did not worked either)

What worked: Implementing the methods within the same view controller.

My Toolbox code:

import Foundation
import UIKit

class Toolbox: UIViewController {

static func tapRecognizerforLabel (named label: UILabel, action: Selector) {
 let tapGestureForLabel = UITapGestureRecognizer(target: self, action: action)
 label.addGestureRecognizer(tapGestureForLabel)
 label.isUserInteractionEnabled = true
 }
}

How I call it:

class ViewOne: UITableViewCell {

@IBOutlet weak var nameLabel: UILabel!

override func awakeFromNib() {
 super.awakeFromNib()
 Toolbox.tapRecognizerforLabel(named: nameLabel, action: #selector(self.methodAlpha))
}

func methodAlpha() {
 print("It's friday my dudes")
}
}

The error I get when touching the label:

2018-07-13 11:08:22.131602-0500 MyApp[20435:1274296] 
+[MyApp.Toolbox methodAlpha]: unrecognized selector 
sent to class 0x1033c0038

2018-07-13 11:08:22.218289-0500 MyApp[20435:1274296] 
*** Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: 
'+[MyApp.Toolbox methodAlpha]: unrecognized selector 
sent to class 0x1033c0038'

Why does it work if I implement the tapRecognizerforLabel() method in ViewOne class but it doesn't if I implement it in other class?

Suggestions to achieve what I want in other ways are welcome

Thank you :)


Solution

  • You need to send the target like this

    func tapRecognizerforLabel (named label: UILabel, action: Selector,target:Any) { 
      let tapGestureForLabel = UITapGestureRecognizer(target:target, action: action)
      label.addGestureRecognizer(tapGestureForLabel)
      label.isUserInteractionEnabled = true
    } 
    

    The target should contain the implementation of the method inside the selector , since you set self which is Toolbox and it doesn't contain it hence the crash happens , to call

    Toolbox.tapRecognizerforLabel(named: nameLabel, action: #selector(self.methodAlpha),target:self)