Swift Unrecognized Selector Sent to Instance Error

Swift unrecognized selector sent to instance error

Two changes for Swift 3:

The selector should look like:

#selector(ClassName.followButtonClick(_:))

The function should have an underscore:

@IBAction func followButtonClick(_ sender: UIButton!) { ...

Notice that these two should be in the same class, otherwise, make sure you initialize the ClassName class.

If you want the selector method(followButtonClick(_:)) to be in the UITableViewCell class. Remove @IBAction(I don't think you need it there):

func followButtonClick(_ sender: UIButton!) { ... 

Unrecognized selector sent to instance 0x7fecc7011000 In Swift

The problem is this outlet:

IBOutlet weak var tableView: UITableView!

You have hooked it up in the storyboard to the wrong object. It is hooked to the cell, not the table view. It looks like you must have hooked it up and then changed the class of the thing it is hooked to.

Swift 4: Unrecognized selector sent to instance when calling a function inside another class

There's no reference to your LoginFunctions from your ViewController. And you pass self as the target to the button but self doesn't have a handleLogin method.

You need to hold and instance of LoginFunctions in your ViewController class. Then pass that reference as the target instead of self.

class ViewController: UIViewController {
let functions = LoginFunctions()

lazy var loginRegisterButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = Color.darkBlue
button.setTitle("Register", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitleColor(UIColor.white, for: .normal)
button.layer.cornerRadius = 5
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)

button.addTarget(functions, action: #selector(LoginFunctions.handleLogin), for: .touchUpInside)

return button
}()
}

unrecognized selector sent to instance 0x7ff0ece07a40 ERROR on xcode

I am quite sure the problem is with my code and not with any connections in the storyboard.

No, the problem is indeed a connection. According to a suggestion in your previous question you renamed Hint to hintButton which does not rename the connection automatically.

In the storyboard delete the dead Hint connection and reconnect it to hintButton.

Unrecognized selector sent to instance when I'm passing it to parent view

Following two functions -

@objc func routeToAddPhotoForm() {
}

@objc func returnToGalleryScreenTab() {
}

should be in your UITabBarController subclass.

The reason is - self points to your UITabBarController subclass here - NOT the other class you are calling it from.

button.addTarget(self, action: selector, for: .touchUpInside)

The other way would be following -

func setLeftNavBarView(target: Any, selector: Selector) {
// Use `target` instead of `self` here
button.addTarget(target, action: selector, for: .touchUpInside)
}

func setRightNavBarView(target: Any, selector: Selector) {
// Use `target` instead of `self` here
button.addTarget(target, action: selector, for: .touchUpInside)
}

From call site -

self.tabBarController?.setLeftNavBarView(target: self, selector: #selector(self.returnToGalleryScreenTab))
self.tabBarController?.setRightNavBarView(target: self, selector: #selector(self.routeToAddPhotoForm))

unrecognized selector sent to instance error in swift

You're creating Selector("updateStopwatch") but your method is called func updateStopWatch().

Use #selector(self.updateStopWatch) instead, this only lets you create selectors on methods that actually exist.

Also make sure that methods referred to by selectors must be visible to the Objective-C runtime, so you need to mark them as @objc.



Related Topics



Leave a reply



Submit