1

I tried to add a target to a UIButton and stumbled upon a weird behaviour

if i try:

//h = a collection view header

switch myVar {

case "none":
    h.button.addTarget(self, action: "buttonTapped:", forControlEvents: .TouchUpInside)

    func buttonTapped(sender:AnyObject) {

    sendFriendRequest(self.targetUser,nil
    } 

}

I get SIGABRT - with "selector not found"

but if I move the function out of the switch case and make it a method of my ViewController, everything works as expected.

Anyone has an explanation for that? Is is just not allowed or are there technical reasons?

longbow
  • 1,593
  • 1
  • 16
  • 39
  • 1
    you only need to make a method of your view controller. because the target is self! which means look for this method in the current class. otherwise specify the object that declares this method. – Omar Al-Shammary May 27 '15 at 21:16

2 Answers2

2

Selectors don't need to be class functions, but they must visible to the object call the selector (i.e. can't be marked as private or inside a method).

InkGolem
  • 2,662
  • 1
  • 15
  • 22
0

You can call any method in the project by using other class's instance instead of self.

Check this answer for the details. https://stackoverflow.com/a/33068386/2125010

Community
  • 1
  • 1
fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88