Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments to #selector method in swift [duplicate]

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?

like image 612
Vinod Avatar asked Jul 12 '16 12:07

Vinod


1 Answers

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.

like image 114
vadian Avatar answered Oct 02 '22 13:10

vadian