Swift add show action to button programmatically
You can go for below code
`
let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
btn.backgroundColor = UIColor.green
btn.setTitle("Click Me", for: .normal)
btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btn.tag = 1
self.view.addSubview(btn)
for action
@objc func buttonAction(sender: UIButton!) {
let btnsendtag: UIButton = sender
if btnsendtag.tag == 1 {
dismiss(animated: true, completion: nil)
}
}
Adding button programmatically swift
Change with
playPauseButton.addTarget(self, action: #selector(playAction), for: .touchUpInside)
this is ok:
@objc
func playAction(sender: UIButton) {
print("Hello")
}
works for me
How do I add a IBAction to a button programmatically in Swift 4?
Swift 4 version:
button.addTarget(self, action: #selector(action(sender:)), for: .touchUpInside)
@objc private func action(sender: Button) {
print("test")
}
Make a UIButton programmatically in Swift
You're just missing the colon at the end of the selector name. Since pressed takes a parameter the colon must be there. Also your pressed function shouldn't be nested inside viewDidLoad.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let myFirstLabel = UILabel()
let myFirstButton = UIButton()
myFirstLabel.text = "I made a label on the screen #toogood4you"
myFirstLabel.font = UIFont(name: "MarkerFelt-Thin", size: 45)
myFirstLabel.textColor = .red
myFirstLabel.textAlignment = .center
myFirstLabel.numberOfLines = 5
myFirstLabel.frame = CGRect(x: 15, y: 54, width: 300, height: 500)
myFirstButton.setTitle("✸", for: .normal)
myFirstButton.setTitleColor(.blue, for: .normal)
myFirstButton.frame = CGRect(x: 15, y: -50, width: 300, height: 500)
myFirstButton.addTarget(self, action: #selector(pressed), for: .touchUpInside)
}
@objc func pressed() {
var alertView = UIAlertView()
alertView.addButtonWithTitle("Ok")
alertView.title = "title"
alertView.message = "message"
alertView.show()
}
EDIT: Updated to reflect best practices in Swift 2.2. #selector() should be used rather than a literal string which is deprecated.
How to create an IBAction for a button programmatically-created based on user input
use addTarget to bind an action to a method
func buttonClicked(sender: UIButton){
print("button Clicked")
}
then add target to button
var button = UIButton()
button.addTarget(self, action: #selector(self.buttonClicked(sender:)), for: .touchUpInside)
How to trigger UIButton action programmatically
UIButton has a method to invoke the targets/selectors that are linked to a certain control event:
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
2022 Swift syntax:
someButton.sendActions(for: .primaryActionTriggered)
How do I create a basic UIButton programmatically?
Here's one:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
Related Topics
Open Uitableview Edit Action Buttons Programmatically
Looping Through Nsattributedstring Attributes to Increase Font Size
Use a Generic Class as a Custom View in Interface Builder
Unexpected Non-Void Return Value in Void Function Swift3
Adding Blur Effect to Background in Swift
How to Create a Simple Checkbox in iOS
Best Way to Parse Url String to Get Values for Keys
Why Does My Programmatically Created Screenshot Look So Bad on iOS 7
Cocoapods Not Installed or Not in Valid State
Is a Date in Same Week, Month, Year of Another Date in Swift
iOS Game Center: Scores Not Showing on Leaderboard in Sandbox
Using Ldap for Authentication in iOS
How Does Xcode Find Implicit Target Dependencies
What Does the Text Inside Parentheses in @Interface and @Implementation Directives Mean
Firebase Cloud Messaging Doesn't Create Push Notifications But Gets Information