Swift Unrecognized Selector Sent to Instance - What Am I Missing

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



Leave a reply



Submit