Search code examples
swiftcosmicmind

Whats the code for button action?


Me again, I'm loving this library but running into minor issues here and there.

For RaisedButton, what code is needed to create an action when the button is create programatically?

btn1.addTarget(self, action: "okButton", forControlEvents: UIControlEvents.TouchUpInside)

func okButton(sender:RaisedButton!) {
    print("button pressed")
}

results in unrecognized selector sent to instance.


Solution

  • In your code remove the unwrapping of the optional parameter:

    func okButton(sender: RaisedButton) {
        print("button pressed")
    }
    

    Add the ":" at the end of your selector name:

    btn1.addTarget(self, action: "okButton:", forControlEvents: .TouchUpInside)
    

    That should solve your issue :)