Unidentified iOS selector sent to instance in Swift

I'm having trouble trying to get UIButton to work when the user clicks on it. I keep getting an error: unrecognized selector sent to instance

override func viewDidLoad() { super.viewDidLoad() button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside) button.setTitle("Print", forState: UIControlState.Normal) button.font = UIFont(name: "Avenir Next", size: 14) button.backgroundColor = UIColor.lightGrayColor() self.view.addSubview(button) } func buttonClick(Sender: UIButton!) { myLabelInfo.text = "Hello" } 

For a Swift method like func buttonClick(Sender: UIButton) , what is the correct line to go to the addTarget method for the selector? Is it "buttonClick", "buttonClick:", "buttonClickSender:" or something else?

+6
source share
6 answers

You are using an invalid method signature for an action. You supply buttonClick , but the method has an argument, so the signature should be buttonClick:

 button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside) 

For more information on how to format your selectors, you can refer to the accepted answer in the message below. The code used in this post may be Objective-C, but all of its lessons can also be applied here.

Creating a selector from a method name with parameters

And as a side note, this code would also be valid if you used Selector("buttonClicked:") as an action, but you don't need it because string literals can be implicitly passed to the Selector type.

Quote from Using Swift with Cocoa and Objective-C

The Objective-C selector is a type that references the name of Objective-C. Objective-C's Swift selector presents a selector structure. You can build a selector with a literal string, for example let mySelector: Selector = "tappedButton:". Because string literals can be automatically converted to selectors, you can pass a string literal to any method that accepts a selector.

+22
source

Swift <2.2

In Swift <2.2, the selector method cannot be private (unrecognized selection error).

The preferred (by Apple) notation is the string notation "methodWithParam:" .

Troubleshooting . If you are having problems with the NSTimer selector, your class may need to be a subclass of NSObject .

Swift> = 2.2

Use the #selector notation. More details here: https://github.com/apple/swift-evolution/blob/master/proposals/0022-objc-selectors.md

For private methods, you can use the @objc method @objc , for example: @objc private func timerTick(timer: NSTimer) .

No need to subclass NSObject anymore!

+11
source

Designation for Swift> 2.2:

 let longPress = UILongPressGestureRecognizer(target: self, action: #selector(YourClass.yourMethod(_:))) 

Worked for me so far (Xcode 7.3)

+3
source

You need to pass the actual Selector . Instead, try using this line:

 button.addTarget(self, action: Selector("buttonClick:"), forControlEvents: UIControlEvents.TouchUpInside) 

You will also need : at the end of the selector name, because you have 1 argument. This is the same as the typical designation for the Obj-C selector.

+2
source

You missed the colon by specifying selector.so the line should be

button.addTarget(self, action: Selector("buttonClick:"), forControlEvents: UIControlEvents.TouchUpInside)

0
source

I had the same problem - the solution turned out to be a prefix of the method that I wanted the button to execute when I clicked with @objc to open it in the Objective-C header file and thereby run Objective-C.

how

 @objc func buttonClick(Sender: UIButton!) { myLabelInfo.text = "Hello" } 
0
source

Source: https://habr.com/ru/post/970662/


All Articles