0

//This is the function which contain my button action programatically

func didSetCategory(info: FitnessCenterModel) {
    myButton.addTarget(self, action: #selector(Function(d: info)), for: .touchUpInside)    
     }

// my selector function for the button target

func Function(d:FitnessCenterModel ) {
print(info)
}

but i am not able to pass as the compiler is throwing an error "Argument of '#selector' does not refer to an '@Objc' method, property, or initialzer

Anil Kumar
  • 945
  • 7
  • 16

1 Answers1

1

A selector does not refer to a method call, but just the method itself.

What you are doing here:

#selector(Function(d: info))

is passing an argument to Function and creating a selector from this. You can't do this. A selector must have no arguments. So how do you pass the info parameter then?

Create a property in your class called selectedCategory:

var selectedCategory: FitnessCenterModel!

Before you call addTarget, assign info to the above property:

selectedCategory = info

Create another method that calls Function with the parameter.

func buttonClicked() {
    Function(d: selectedCategory)
}

Change your addTarget call to use a buttonClicked as a selector:

#selector(buttonClicked)
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thanks@sweeper, would you tell me more about this problem..? – Anil Kumar Sep 19 '17 at 06:14
  • @AnilKumar What problem? Do you have as specific question that is related post? If yes then feel free to ask! – Sweeper Sep 19 '17 at 06:16
  • Like the question is we can pass a sender as a parameter to selector , but why can't we pass a parameter the same way..? – Anil Kumar Sep 19 '17 at 06:17
  • @AnilKumar _You_ don't pass the sender. The _button_ passes the sender. As the provider of a selector, you don't pass anything. – Sweeper Sep 19 '17 at 06:20
  • so passing a sender is optional ..? – Anil Kumar Sep 19 '17 at 06:21
  • @AnilKumar By doing `#selector(someFunc(sender:))` you are not passing the sender. The `sender:` bit is actually pretty redundant unless you have multiple functions called `someFunc`. Writing "`sender:`" is optional but the button will always pass a sender. If your function does not have a parameter, then the passed sender will just get discarded. – Sweeper Sep 19 '17 at 06:24
  • Thank you for explaining the concept and giving your valuable time..:-) – Anil Kumar Sep 19 '17 at 06:26