LK Phuc Nguyen LK Phuc Nguyen - 1 year ago 76
Swift Question

NSInvalidArgumentException with button tap detection

The app has a screen in which after giving the inputs, I tap OK button and it should perform input validation, if fine, then navigate and pass data back to the previous screen via protocol/delegate, otherwise, block navigation and display a warning pop-up.

This is my viewDidLoad:

override func viewDidLoad() {

button.addTarget(self, action: "pushView:", forControlEvents: .TouchUpInside)

This is the pushView() function:

func pushView() {
if (!inputCheck(remTitle.text!) || !inputCheck(remDesc.text!) || dueDate == nil) {

let alertController = UIAlertController(title: "Some fields are empty!", message: "Please fill all the fields...", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
self.presentViewController(alertController, animated: true, completion: nil)

let red: UIColor = UIColor(red: 255.0/255, green: 100.0/255, blue: 50.0/255, alpha: 1.0)

if (!inputCheck(remTitle.text!)) {
remTitle.text = "Required"
remTitle.textColor = red

if (!inputCheck(remDesc.text!)) {
remDesc.text = "Required"
remDesc.textColor = red
else {
let aReminder = Reminders(title: remTitle.text!, dueDate: self.dueDate!, desc: remDesc.text!)


When I run the app in simulator, it crash when I tap the button and this is the error message:

2016-04-21 00:12:39.976 ToDo List[1795:1253192] -
[ToDo_List.AddReminderController pushView:]:
unrecognized selector sent to instance 0x7fa13ac16c40
2016-04-21 00:12:39.981 ToDo List[1795:1253192]
*** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-
[ToDo_List.AddReminderController pushView:]:
unrecognized selector sent to instance 0x7fa13ac16c40'

There may be an issue with sender or selector but I don't know what it exactly means.

Answer Source

When you add a : to your selector/action name like you have here:

button.addTarget(self, action: "pushView:", forControlEvents: .TouchUpInside)

It means that your selector expects a parameter.

Your pushView() function on the other hand, does not include any parameters, so to iOS/UIKit you are referencing two different things.

The solution is to:

  • either remove the : from your action like so:

button.addTarget(self, action: "pushView", forControlEvents: .TouchUpInside)

  • or add a parameter to your pushView() function like so:

func pushView(sender: AnyObject)