emre doğmaz
emre doğmaz

Reputation: 75

Argument of '#selector' does not refer to an '@objc' method, property, or initializer in Swift button target

i want to send parameter to my details func with selector but it does not work, how can i do this?

btnDetails.addTarget(self, action:#selector(goToDetailsFromMap(mid:id)), for: .touchUpInside)

Upvotes: 0

Views: 524

Answers (2)

valosip
valosip

Reputation: 3402

You can create a custom button:

class CustomButton: UIButton {
    var someId: Int?
    ...
}

Then in your viewController

    ...    
    btnDetails.someId = ...
    btnDetails.addTarget(self, action:#selector(didTapCustomButton(_:)), for: .touchUpInside)
    ...

@objc func didTapCustomButton(_ sender: CustomButton) {
    if let id = sender.someId {
        //Do something with id
    }
}

Upvotes: 1

wammy
wammy

Reputation: 53

adding selector to button with code using swift 5.0

myButton.addTarget(self, action:#selector(myButtonTapped), for: .touchUpInside)

function declaration

 @objc func myButtonTapped() {
      //do stuff here
    }

Upvotes: 0

Related Questions