1

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() {
    super.viewDidLoad()

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

This is the pushView() function:

func pushView() {
    //some codes to validate data and perform navigation
}

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.

Jay Nguyen
  • 342
  • 1
  • 2
  • 18

2 Answers2

0

Fix selector:

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

For future remember that latest swift 2.2 syntax has some differences in this.

button.addTarget(self, action: #selector(MyClass.pushView), forControlEvents: .TouchUpInside)
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
0

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)

pbodsk
  • 6,787
  • 3
  • 21
  • 51
  • Thank you very much for your explanation! I am very new to Swift. – Jay Nguyen Apr 20 '16 at 15:35
  • You're welcome. Selectors have an odd syntax until you understand it. This answer talks a bit more about selectors and their syntax: http://stackoverflow.com/a/6404439/4063602. And as @oleg-gordiichuk mentions in his answer, the syntax has been changed in Swift 2.2 so you can write `#selector` instead and have code assist to help you select the right method. This is probably because so many had problems with the "old" selector syntax like you just had (and like I've had too) – pbodsk Apr 21 '16 at 06:19