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!) { ...
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
}()
}
Thread 1: Exception:unrecognized selector sent to instance 0x7fb12cc088c0 exception error
This can happen if you add a selector (a function connected to an event) through the storyboard and then change it in code, resulting in the storyboard being out of sync.
Assuming you're trying to connect buttonAction(sender:UIButton)
with your recordButton
, try doing this:
- Mark your function as @IBAction. (maybe you deleted this by mistake after connecting it to the storyboard?)
@IBAction func buttonAction(sender: UIButton)
- Go to the storyboard and delete the existing connection by going to the connections inspector after selecting the button and then deleting any connections (in my screenshot, I would click the x next to View Controller / Your Function):
- Reconnect the function to the button by dragging the circle next to the event you want to connect to and and dropping over your view controller, and then selecting your buttonAction function.
If this doesn't work try providing some screenshots of your storyboard connections, I'll try to help out more!
unrecognized selector sent to instance-Swift
In target-action pattern, the target object needs to implement the action method.
But in your code:
self.callFuncTimer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.busInfoObject.downloadBusData(userId:warning:delegate:completed:)), userInfo: nil, repeats: true)
You use self
as target, which is an instance of MapVC
, that does not implement the method downloadBusData(userId:warning:delegate:completed:)
.
When you specify #selector(someInstance.methodName(...))
for the action method, you need to pass someInstance
to the target object. In your case someInstance
is self.busInfoObject
.
Which means the line creating a Timer
should become like this:
self.callFuncTimer = Timer.scheduledTimer(timeInterval: 5, target: self.busInfoObject, selector: #selector(self.busInfoObject.downloadBusData(userId:warning:delegate:completed:)), userInfo: nil, repeats: true)
But this does not work.
I was stupid enough that I have almost forgotten to tell you another important thing in target-action pattern.
Which is,
The signature of the action method is definitely fixed according to the target.
When using Timer
, the signature needs to be the same as this:
class func scheduledTimer(timeInterval: TimeInterval, target: Any, selector: Selector, userInfo: Any?, repeats: Bool) -> Timer
- (void)timerFireMethod:(NSTimer *)timer
The notation is in Objective-C format, but the action method for Timer
needs to have one and only one argument of type Timer
(it's NSTimer
in Objective-C.)
So, you may need to define a method matches the signature in your MapVC
:
func timerFired(_ timer: Timer) {
self.busInfoObject.downloadBusData(userId: ...,
warning: {_, _, _ in
...
},
delegate: self,
completed: {
...
})
}
And change the line setting the timer to:
self.callFuncTimer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.timerFired(_:)), userInfo: nil, repeats: true)
Sorry, for showing you an incomplete answer, please try with filling ...
in my updated code.
IOS: Unrecognized selector sent to instance Swift class in Objective C
The problem was in the viewController casting, it works fine after unchecked inherit module from target
Storyboard class
Unrecognized selector sent to instance for Bar Button
Verify the IBOutlets
and IBActions
are properly connected. These crash happens when the XIB
and Class
files are not properly connected.
Try to remove the connections and add it again. Also rename the property names if the error persist.
Related Topics
How to Use Afnetworking or Sthttprequest to Make a Request of a Soap Web Service
Error: Uitableview Jump to Top with Uitableviewautomaticdimension
Libz.Dylib Versus Libz.1.2.3.Dylib Versus Libz.1.2.5.Dylib
How to Read Incoming Sms by Using Application in iOS
Apple MACh-O Linker (Id) Warning:Building for MACosx, But Linking Against Dylib Built for iOS
Swift 3 - Alamofilre 4.0 Multipart Image Upload with Progress
How to Show "Would Like to Send You Push Notifications" Alert View Again
Multiple Uicollectionview in One Controller
Non-Nominal Type X Does Not Support Explicit Initialization
Ios: Usage of Self and Underscore(_) with Variable
How to Parse Array of JSON to Array in Swift
Is Iboutletcollection Guaranteed to Be of Correct Order
Track Cellular Data Usage Using Swift
Ios7 - Device Unique Identifier
Swiftui How to Align a View That's Larger Than Screen Width
iOS 5 Gm: <Error>: More Than Maximum 5 Filtered Album Lists Trying to Register. This Will Fail