Swift programmatically create function for button with a closure
In Swift you can create a function for a button like this: button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside) However is there a way I can do something like this: button.whenButtonIsClicked({Insert code here}) That way I do not even have too declare an explicit function for the button. I know I can use button tags but I would prefer to do this instead. Create your own UIButton subclass to do this: class MyButton: UIButton { var action: (() -> Void)? func whenButtonIsClicked(action: @escaping () -> Void) { self.action = action self.addTarget(self, action: