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!
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
UIButton
inherits fromUIControl
. That has all of the methods to add/remove actions: UIControl Class Reference. Look at the section "Preparing and Sending Action Messages", which covers– sendAction:to:forEvent:
and– addTarget:action:forControlEvents:
.Try this:
Swift 4
Objective-C
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
inaction:@selector(myAction)
Here's the reference.
Swift answer:
Documentation: https://developer.apple.com/documentation/uikit/uicontrol/1618259-addtarget
try this:
first write this in your .h file of viewcontroller
Now write this in your .m file of viewcontrollers viewDidLoad.
write this outside viewDidLoad method in .m file of your view controller
For Swift 3
Create a function for button action first and then add the function to your button target