from How to create a button programmatically?
self doesn\'t work in swift playground :
button.addTarget(self, action: \"buttonAction:\", forControlE
I do this successfully with Cocoa.
Make your class a subclass of NSObject. Create an NSWindow in your class and move the button and the action into your class. Then you can set the button's target to self.
Try it with UIKit and let us know if it works.