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
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)
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 (swift 3)
What you are trying to do isn't possible with Swift's #selector
. Instead, insert instances of the Selector
class directly into the dictionary.
You'll also need to annotate with @objc
@objc // This annotation isn't required in Swift 3, but will be in Swift 4
func didSwipeRight() {}
var functionForRecognizer: [UISwipeGestureRecognizer : Selector] {
return [
rightSwipeRecognizer : #selector(didSwipeRight)
]
}
recognizer.addTarget(self, action: functionForRecognizer[recognizer]))
For a detailed explanation of #selector
see this answer. Summary, #selector
is a compiler construct for Swift that ensures the exact selector/method exists, including the class it should exist on. It looks at the actual contents within its parentheses to see if it is a known method, and functionForRecognizer[recognizer]
is not a method name, it is a variable + subscript method call.
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.
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 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
Underlying Type for Tuple in Swift
How to Make a Function Operate on a Sequence of Optional Values
Why Does My Swift Bundle Get The Wrong Principal Class
Uibutton Borders Function Only Gives Back White Borders
Do Swift Hashable Protocol Hash Functions Need to Return Unique Values
Why Is Swift Giving Me Inaccurate Floating Point Arithmetic Results
Passing Values Between Viewcontrollers Based on List Selection in Swift
Typecase Regular Swift Function to Curry Function
Swift Applications Takes More Space on Disk
Continuous Rotation of Nsimageview (So It Appears to Be Animated)
Firestore - Creating a Copy of a Collection
How to Add UIpickerview in UIalertcontroller
Swift Optionals Best Practices