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 forth constantly. The solution is probably really simple, but I just can't seem to find any answers when I search it. Thank you!
Try this:
Swift 4
myButton.addTarget(self, action: #selector(myAction), for: .touchUpInside)
Objective-C
[myButton addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside];
You can find a rich source of information in Apple's Documentation. Have a look at the UIButton's documentation, it will reveal that UIButton is a descendant of UIControl, which implements the method to add targets.
--
You'll need to pay attention to whether add colon or not after myAction
in action:@selector(myAction)
Here's the reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With