5

I want to pass multiple argument to a function while I click an image. Here is my code

var param1 = 120
var param2 = "hello"
var param3 =  "world"

let image: UIImage = UIImage(named: "imageName")!
 bgImage = UIImageView(image: image)

let singleTap = UITapGestureRecognizer(target: self, action:#selector(WelcomeViewController.tapDetected(_:secondParam:thirdParam:)))
singleTap.numberOfTapsRequired = 1
bgImage!.userInteractionEnabled = true
bgImage!.addGestureRecognizer(singleTap)

calling function

func tapDetected(firstParam: Int, secondParam: String, thirdParam: String) {
print("Single Tap on imageview")
            print(firstParam)  //print 120
            print(secondParam) // print hello
            print(thirdParam) /print world
        }

How can I pass arguments so that I can get correct values?

Vinod
  • 675
  • 3
  • 7
  • 25

2 Answers2

4

You can't. From the documentation:

A gesture recognizer has one or more target-action pairs associated with it.
...
The action methods invoked must conform to one of the following signatures:

- (void)handleGesture;
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

You may use instance variables to pass the parameters.

Community
  • 1
  • 1
vadian
  • 274,689
  • 30
  • 353
  • 361
  • How can I use instance variable in my particular case ? – Vinod Jul 13 '16 at 04:21
  • It's practically the same as suggested in Shadow Of's answer. – vadian Jul 13 '16 at 04:23
  • Ok, Just tried the option, subclass UIGestureRecognizer and add required property . It worked. class MyTapGestureRecognizer: UITapGestureRecognizer { var headline: String? } http://stackoverflow.com/questions/35635595/sending-a-parameter-argument-to-function-through-uitapgesturerecognizer-selector – Vinod Jul 13 '16 at 04:34
  • Actually *instance variables* means not to subclass `UITapGestureRecognizer` but rather create the properties in the target class where the recognizer is used. – vadian Jul 13 '16 at 04:38
2

In swift2 you can't pass parameters in actions. You may just use class properties:

var param1 = 120
var param2 = "hello"
var param3 = "world"
let image: UIImage = UIImage(named: "imageName")!
var bgImage: UIImageView?

override func viewDidLoad() {
    super.viewDidLoad()

    bgImage = UIImageView(image: image)
    let singleTap = UITapGestureRecognizer(target: self, action: #selector(WelcomeViewController.tapDetected))
    singleTap.numberOfTapsRequired = 1
    bgImage!.userInteractionEnabled = true
    bgImage!.addGestureRecognizer(singleTap)
}

calling function

func tapDetected() {
    print("Single Tap on imageview")
    print(param1) // print 120
    print(param2) // print hello
    print(param3) // print world
} 
Yury
  • 6,044
  • 3
  • 19
  • 41
  • in this URL they are mentioning something related to multiple arguments in swift 2.2 http://stackoverflow.com/questions/36166248/understanding-swift-2-2-selector-syntax-selector – Vinod Jul 12 '16 at 12:50
  • @Vinod You can define Selector with any number/names of arguments, but we need from UITapGestureRecognizer ability to support them, to call selector with some specified parameters, and UITapGestureRecognizer have no this ability – Yury Jul 12 '16 at 13:35