UIButton causing unrecognized selector sent to instance
Your Selector(("buttonAction:"))
might be the cause of your problem.
Try #selector(buttonAction(sender:))
instead.
Unrecognized selector sent to instance error when pressing UIButton in Xcode 8/Swift 3
It seems like you have rename your IBAction
method name earlier it was different and it is connected to the previous name inside your storyboard. So just disconnect your action method and reconnect it appropriately.
Unrecognized selector sent to instance in UIButton
One problem is that you are forcing manual formation of the Objective-C selector, and you don't actually know how to form an Objective-C selector manually so you are getting it wrong. Don't do that! Let the compiler form the selector for you. That's its job. Replace
action: Selector(("deleteUser"))
with
action: #selector(deleteUser)
Also, you need to expose your deleteUser
method to Objective-C explicitly:
@objc func deleteUser(_ sender: UIButton) {
Otherwise Objective-C still won't be able to introspect your class and find this method when the time comes to call it. Fortunately, when you switch to #selector
syntax, the compiler will call out that issue for you!
Swift Customized UIButton unrecognized selector sent to instance
If you'd share the FULL error message, you should have:
-[TheClass tapAction:] unrecognized selector sent to instance
Where TheClass
should be the class of the instance that called tapInside(target:action:)
.
That's what might give you a tip on your issue.
Ie, TheClass
is calling on itself the method tapAction(_:)
, which is doesn't' know.
It's like writing theClass.tapAction(someSender)
, this shouldn't compile, right?
The issue, is that in addTarget(_:action:for:)
, the target
is the one that implement the action
(selector). In this case, it's self
, the BaseButton
instance.
So:
self.addTarget(target, action: #selector(tapAction(_:)), for: .touchUpInside)
=>
self.addTarget(self, action: #selector(tapAction(_:)), for: .touchUpInside)
Now, since you don't need anymore the target
parameter, you can remove it from the method:
public func tapInside(target: Any?, action: ((UIButton)->())?) {...}
=>
public func tapInside(action: ((UIButton)->())?) {...}
UIButton unrecognized selector sent to instance
Try this
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! ClinicListCell
cell.title.text = clinicNames[indexPath.row]
cell.subTitle.text = clinicSubs[indexPath.row]
cell.backgroundImageView.image = UIImage(named: clinicImages[indexPath.row])
cell.profileBtn.tag = indexPath.row
cell.profileBtn.addTarget(self, action: #selector(YourViewController.profileBtnClicked(sender:)), for: UIControlEvents.touchUpInside)
return cell
}
class func profileBtnClicked(sender:UIButton) {
print("Selected")
}
UIButton unrecognized selector sent to instance when using code in new ViewController
Well, I still don't know exactly what caused this issue but a desperate attempt to fix it resulted in me updating to Swift 3 from 2.3, fixed the issue immediately.
UIButton in table cell unrecognized selector sent to instance
Seems like the problem is about forwardPress
, not forwardButton
nor cellButtonPress
. Did you check the Outlet Inspector in the Interface Builder?
On some interface element (maybe the cell when reading the debugger), you may have an outlet not linked in code called forwardPress
. You perform the action on the element, IB looks for the forwardPress
method, which does not exist => crash.
UIButton creating error - unrecognized selector sent to instance
You should modify addTarget:
button.addTarget(self, action:Selector("buttonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
Related Topics
Presenting a Uiviewcontroller from Skscene Shows Black Screen
With Data (Not Nsdata), in Fact How Actually Do You Make a Utf8 Version of a Jpeg
Firebase Datadescription Returns Empty Array
How to Make a Uiview Grow in Uiview.Animate() with an Accurate Corner Radius
Access Enum Associated Value as Optional
Fetch an Email Body in Mailcore2 Osx with Swift
How to Generate a Random Unicode Character in Swift
Different Path Url for Filemanager Everytime I Open the App
Url Constructor Doesn't Work with Some Characters
Xcode 7.1 Beta: Content of File Error
How to Make a Variable from a String
Checkboxes in Uitableview State Persistence
Where to Implement Nsvaluetransformer for Core Data in Swift
Why Function Return Nil Firebase Swift
Updated Approach to Reauthenticate a User
Parse.Com Pfquery Order by Time (Swift)
Check Whether the Arreferenceimage Is No Longer Visible in the Camera's View
Getting the Time Remaining in the Time Interval of a Timer in Swift