Add action to UIBarButtonItem Swift

93sauu picture 93sauu · Aug 31, 2014 · Viewed 14.3k times · Source

Button is created in storyboard and I want to add an action.

self.cancel.action = NSSelectorFromString("cancel:")

func cancel(sender: UIBarButtonItem) ->() {
}

This not working. Thanks

Answer

Mundi picture Mundi · Aug 31, 2014

Just create a function in your swift class like the following

@IBAction func cancel() {
   // your code
}

And connect it in storyboard by ctrl-dragging from the button to your view controller.