For the More Curious: Setting the Target Programmatically
NSControl includes the property:
var action: Selector
Notice that the action property is a selector, so to set the action of a control programmatically, you need to create a selector. For example, suppose you had a button playButton and a method play:
var playButton: NSButton func play(sender: NSButton) { ... }
To set playButton’s action to be play, you would write:
let playSelector = Selector("play:") playButton.action = playSelector
The first line initializes a Selector with the string
"play:" ...
Get Cocoa Programming for OS X: The Big Nerd Ranch Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.