Swift unrecognized selector sent to instance - What am I missing?
Your buttonAction
target is nested inside your viewDidLoad()
method. Move it outside and it should be reachable.
class ViewController: UIViewController {
func buttonAction(sender:UIButton)
{
println("Button tapped")
}
override func viewDidLoad() {
super.viewDidLoad()
// ...
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
// ...
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
myView.addSubview(button)
}
}
Swift unrecognized selector sent to instance on button click
The issue is that you are not adding action to button in correct way. Please do it like that
button.addTarget(self, action: #selector(buttonAction), for: UIControlEvents.touchUpInside)
Swift: unrecognized selector sent to instance
If I am not wrong you have declared your backButtonPressed
method inside another method like this:
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRectMake(150, 240, 75, 30))
button.setTitle("Next", forState: UIControlState.Normal)
button.addTarget(self, action: Selector("backButtonPressed:"), forControlEvents: UIControlEvents.TouchUpInside)
button.backgroundColor = UIColor.greenColor()
self.view.addSubview(button)
func backButtonPressed(sender:AnyObject?) {
print("Called")
}
// Do any additional setup after loading the view, typically from a nib.
}
This is wrong way.
Declare your method outside as shown in below code:
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRectMake(150, 240, 75, 30))
button.setTitle("Next", forState: UIControlState.Normal)
button.addTarget(self, action: Selector("backButtonPressed:"), forControlEvents: UIControlEvents.TouchUpInside)
button.backgroundColor = UIColor.greenColor()
self.view.addSubview(button)
}
func backButtonPressed(sender:AnyObject?) {
print("Called")
}
Swift selector - unrecognized selector sent to instance
Check the documentation for scheduledTimerWithTimeInterval
You will see that
The selector should have the following signature:
timerFireMethod:
(including a colon to indicate that the method takes an argument).The
timer passes itself as the argument, thus the method would adopt the
following pattern:
- (void)timerFireMethod:(NSTimer *)timer
Your function doesn't match this pattern, so the appropriate selector cannot be found.
Use something like -
func runRockRotationForTimer(_ timer: NSTimer){
self.runRockRotation(timer.userInfo as? SKSpriteNode)
timer.invalidate();
}
and schedule it using
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotationForTimer:", userInfo: rock, repeats: false)
Unrecognized selector sent to instance tableview swift
Try the following:
cell.acceptBtn.addTarget(self, action: "acceptRequest:", forControlEvents: .TouchUpInside)
… noting that it is "acceptRequest:"
and not "acceptRequest"
. The latter implies that the function has no parameters.
Xcode gives an error Unrecognized selector sent to instance”
According to the exception reason (you should always include those errors in the question) in the comments there is a dead connection in Interface Builder
- Press ⇧⌘F
- Search for addOperation
- Click on the search result
- Delete the connection in Interface Builder
If nothing is found press ⇧⌘K to clean the project
Related Topics
Cannot Call Value of Non-Function Type 'Module<Firebase>'
Ar Refernce Image Plane Was Not Position Properly in iOS Swift
Force Wkwebview to Show Mobile Version
Receive Signal from Beacon While App Is in the Background
Hkanchoredobjectquery Not Returning Reliably in Background
Ad-Hoc Distributed Application Failed to Launch in Time
iPhone /iOS: Presenting HTML 5 Keyboard for Postal Codes
How to Detect If The HTML5 Autoplay Attribute Is Supported
Allow Zooming Within Iframe But Not on Page in iOS
Fixed Background for Part of a Site (Div) in iOS
Why Does 'Position:Fixed' Not Work When Viewed in an 'Iframe' Using an iPhone or iOS Device
Add Udid in Current Provisioning Profile
Retina Display Vs Normal Display Color Difference
Simulator Vs Physical Device: Navigationlink Broken After One Use
How to Upload Multiple Images in Multipart Using Alamofire
How to Print Out(Nslog) The Properties of a Custom Object Added to a Nsmutablearray