How do you add an action to a button programmatically in xcode
Solution 1
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.
Solution 2
Swift answer:
myButton.addTarget(self, action: "click:", for: .touchUpInside)
func click(sender: UIButton) {
print("click")
}
Documentation: https://developer.apple.com/documentation/uikit/uicontrol/1618259-addtarget
Solution 3
CGRect buttonFrame = CGRectMake( 10, 80, 100, 30 );
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: @"My Button" forState: UIControlStateNormal];
[button addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[view addSubview:button];
Solution 4
try this:
first write this in your .h file of viewcontroller
UIButton *btn;
Now write this in your .m file of viewcontrollers viewDidLoad.
btn=[[UIButton alloc]initWithFrame:CGRectMake(50, 20, 30, 30)];
[btn setBackgroundColor:[UIColor orangeColor]];
//adding action programatically
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
write this outside viewDidLoad method in .m file of your view controller
- (IBAction)btnClicked:(id)sender
{
//Write a code you want to execute on buttons click event
}
Solution 5
CGRect buttonFrame = CGRectMake( x-pos, y-pos, width, height ); //
CGRectMake(10,5,10,10)
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
button setTitle: @"My Button" forState: UIControlStateNormal];
[button addTarget:self action:@selector(btnClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor: [UIColor BlueVolor] forState:
UIControlStateNormal];
[view addSubview:button];
-(void)btnClicked {
// your code }
Related videos on Youtube
aggiesfan64
Updated on September 27, 2020Comments
-
aggiesfan64 over 3 years
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!
-
acecapades about 12 yearsWhat if I want to pass a parameter to the selector when my button is pressed (eg. I have a button inside a UITableViewCell and I want to pass the object in that cell when the cell's button is pressed)?
-
Nick Weaver about 12 years@acecapades I think you are mixing two things. You can't attach a paramater like you could do with performSelector. The action pattern used by the UIControl descendant UIButton is to notifiy some target with a certain selector when controlEvent is triggered. When this happens in your case you could go up the superviews to see who is encapsulating your button and ask a method to resolve the tablecell to model relation and then do whatever you want with that.
-
acecapades about 12 yearsI see. That makes sense. I think I understand what you're saying. Thanks for the tip Nick!
-
Nogard over 10 yearsplease provide some comments explaining how your sample can help.
-
user2677311 over 10 yearsUIButton *btnNotification=[UIButton buttonWithType:UIButtonTypeCustom]; btnNotification.frame=CGRectMake(130,80,120,40);//Button Frame Setting btnNotification.backgroundColor=[UIColor greenColor]; [btnNotification setTitle:@"create Notification" forState:UIControlStateNormal];//Set the title for button [btnNotification addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];//add Target For button [self.view addSubview:btnNotification];} -(void)btnClicked { // Here You can write functionality }
-
user2677311 over 10 years-(void)btnClicked { //here what ever you want you can set the action for button //Example:go to next viewcontroller nextviewController *nextPage=[nextviewController alloc]init]; [self.navigationController pushviewcontroller:nextPage animated:YES ]; }
-
ErasmoOliveira almost 9 yearsis it possible to add 2 or more actions?
-
dumbledad over 8 yearsWhat's the ":" for after "click" in the call to UIControl.addTarget?
-
Etan over 8 yearsSelectors have a
:
as placeholders for function arguments. Sinceclick
takes thesender
argument, that is replaced with a:
in the selector string. -
Admin almost 8 yearscan someone translate this in swift?
-
uplearned.com over 7 yearsThis worked recently for me for Swift 3. button.addTarget(self, action: #selector(ViewController.click), for: UIControlEvents.touchUpInside) func click() { print("click") } Thanks @Glauco Neves for pointing us down the right track