1

In my old Swift iOS app project I use code like his:

button!.addTarget(self,action:"ratingButtonTapped:",for:.touchDown)

where ratingButtonTapped is a function in the same class

The code converter gives error

"No method declared with Objective-C selector 'ratingButtonTapped:'"

and then suggests this solution button!.addTarget(self,action:Selector("ratingButtonTapped:"),for:.touchDown)

The only problem is that even after applying the fix, it continues to give warning

"No method declared with Objective-C selector 'ratingButtonTapped:'"

where is then suggest to wrap it in parentheses to hide the warning

...

This is my function declaration in the same classd:

func ratingButtonTapped(_ sender: UIButton) {
}

...

I guess the way I did this dated and wrong in Swift 3 - but what is the correct way then? The class has a function with name ratingButtonTapped

Tom
  • 3,587
  • 9
  • 69
  • 124

1 Answers1

1
 button.addTarget(self, action: #selector(YourClassController. ratingButtonTapped(_:)), for: .touchUpInside)

 func ratingButtonTapped(_ sender:UIButton){
 }
Punit
  • 1,330
  • 6
  • 13
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – DimaSan Mar 07 '17 at 11:05
  • I tried button!.addTarget(self, action: #selector(self.ratingButtonTapped(sender:)), for: .touchDown) but I get error: Value of type 'RatingControl' has no member 'ratingButtonTapped(sender:) Maybe somehow I need to change method body to work with selectors? – Tom Mar 07 '17 at 11:56
  • what is your classname? – Punit Mar 07 '17 at 11:59
  • Answer accepted - I had an error - I should have used #selector(self.ratingButtonTapped(_:)), for: .touchDown) (as you wrote) and not #selector(self.ratingButtonTapped(sender:)), for: .touchDown) – Tom Mar 07 '17 at 12:01