Argument of '#Selector' Does Not Refer to an '@Objc' Method, Property or Initializer

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

All you have to do is mark the func as @objc and there's no need for the self reference or the parenthesis

class ActionButton: JTImageButton {

@objc func btnAction() {

}

func configure() {
// ...
self.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
// error:
// Argument of '#selector' does not refer to an '@objc' method, property, or initializer

}
}

You can even make it private if you want

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

The selector of a target / action method must be declared either without parameter or with one parameter passing the affected object.

In case of a Timer use the userInfo parameter to pass data.

timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector:#selector(updateTimer(_:)), userInfo: 3, repeats: true)   

func updateTimer(_ timer: Timer) {
let endTime = timer.userInfo as! Int
counter -= 1
timeLabel.text = String(counter)
if counter == endTime {
step += 1
}
}

If the enclosing class does not inherit form NSObject you have to add the @objc attribute to the action method.

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

Selector() : It receives string/ string-literal as a parameter. It was used previously and #selector is upgraded definition.

#selector : You are trying to pass a string in #selector whereas it takes Objective-C type function reference. Correct example is:

NotificationCenter.default.addObserver(self, selector: #selector(peerChangedNotification(notification:)), name: NSNotification.Name("MPC_DidChangeStateNotification"), object: nil)

Adding an @objc method as an action to a button and getting Argument of '#selector' does not refer to an '@objc' method, property, or initializer

The signature that you are using doesn't match your objc method. The correct selector would be touchButton(_:), not touchButton(button).

Argument of '#selector' does not refer

Selectors can't be called like that. You don't get to pass the arguments for a selector, the API does.

You have to pass in:

#selector(datePickerValueChanged)

This means that your datePickerValueChanged method can't take 2 arguments, because the API expects only one.

This means that you need a way to know which text field's date picker changed. One simple way to do this is to create a property called focusedTextField:

var focusedTextField: UITextField!

Set this to sender in your dataPicker method:

focusedTextField = sender

And set it to nil when the user ends editing

// in another method that is called when the text field ends editing
focusedTextField = nil

Now, you can remove the second argument from datePickerValueChanged and use focusedTextField instead.

Argument of '#selector' does not refer to an initializer or method

You can't have the action parameter specify an arbitrary expression. It needs to invoke a specific selector - a class and function in that class.

You will need to create a function in your class that invokes the delegate method:

class FormulaInfoViewController: UIViewController, Dismissable {

weak var dismissalDelegate: DismissalDelegate?
override func viewDidLoad() {
super.viewDidLoad()

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: #selector(FormulaInfoViewController.selectionDidFinish)
}

@objc func selectionDidFinish() {
self.dismissalDelegate?.selectionDidFinish(self)
}
}


Related Topics



Leave a reply



Submit