Tom
Tom

Reputation: 3627

Swift3 and addTarget Selector code converter suggestion

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

Upvotes: 1

Views: 172

Answers (1)

Punit
Punit

Reputation: 1340

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

 func ratingButtonTapped(_ sender:UIButton){
 }

Upvotes: 1

Related Questions