I know how to add an IBAction to a button by dragging from the interface builder, but I want to add the action programmatically to save time and to avoid switching back and
For Swift 3
Create a function for button action first and then add the function to your button target
func buttonAction(sender: UIButton!) { print("Button tapped") } button.addTarget(self, action: #selector(buttonAction),for: .touchUpInside)