7

I write with Swift 4 and try to write a function that send categoryId as action but I couldn't write .I think my syntax is wrong.If I write function without parameters its not a problem but I get error with parameters functions. Could you say me how to use selector?

@objc func sendCategoryIdToPackageSelectionVC(categoryId : Int){
        MarketVC.categoryId = categoryId
        self.performSegue(withIdentifier: "sequeGoToPackageSelection", sender: nil)
    }

func addTapFeatures(){
        taplabel1 = UITapGestureRecognizer(target: self, action: #selector(self.sendCategoryIdToPackageSelectionVC(categoryId:2)))
        taplabel1?.cancelsTouchesInView = false
        self.labelFirst.addGestureRecognizer(taplabel1!)
}

I get an error saying that the action selector doesn't refer to any objc method.

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
Akif Demirezen
  • 191
  • 1
  • 1
  • 13
  • Check [this thread](https://stackoverflow.com/q/43251708/6541007) or [this](https://stackoverflow.com/a/24814958/6541007). Nothing new in Swift 4 about the usage of `#selector`. – OOPer Nov 18 '17 at 09:42
  • This might help: https://stackoverflow.com/questions/19703767/pass-parameter-to-uitapgesturerecognizer – Ozgur Vatansever Nov 18 '17 at 09:50
  • func sendCategoryIdToPackageSelectionVC(categoryId : Int){ self.performSegue(withIdentifier: "sequeGoToPackageSelection", sender: nil) } @objc func callMethod(){ sendCategoryIdToPackageSelectionVC(categoryId: 2) } func addTapFeatures(){ taplabel1 = UITapGestureRecognizer(target: self, action: #selector(self.callMethod)) taplabel1?.cancelsTouchesInView = false } – Yongjoon Nov 18 '17 at 10:00
  • Thank all of you guys, I noticed that my opinion is wrong . I will use same function again but , this time I use sender : UILabel in objc function, after that I control sender.tag which label's belongs to this tag.And I send categoryId according to that Thanks your helps . – Akif Demirezen Nov 18 '17 at 10:07

1 Answers1

2

i think, you can't directly use selector to pass parameter. try some thing like this you should create an @objc method that calls handleTap(modelObj:myModelObj).

@objc func someMethod() { // name this properly!
    handleTap(modelObj: myModelObj)
}

Then you can pass this as a selector:

#selector(someMethod)