10

I am new to iPhone technology. Please anyone tell me how to add action for UIButton.

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 25);
btn.backgroundColor = [UIColor clearColor];
[btn setTitle:@"Play" forState:UIControlStateNormal];
[btn addTarget:self action:(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
btn.center = self.center;
[self addSubview:btn];
meh-uk
  • 2,031
  • 1
  • 23
  • 36
Ankur
  • 279
  • 1
  • 2
  • 9
  • Not about the question but you have a memory leak here. don't call retain on the button if you don't call release on it later. The retain is not needed as the button is not an ivar and is autoreleased. – Zoleas Dec 01 '11 at 13:17
  • Removed the memory leak. – meh-uk Sep 24 '15 at 10:42
  • 1
    [IOS 14 SDK: you can add action with closure callback:](https://stackoverflow.com/questions/5843427/how-do-you-add-an-action-to-a-button-programmatically-in-xcode/640871599#answer-64087159) – Ucdemir Sep 27 '20 at 10:01

7 Answers7

11

Use This

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self 
       action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [view addSubview:button];
Wolverine
  • 4,264
  • 1
  • 27
  • 49
7

You specify selectors using @selector(). So, the action parameter looks like,

action:@selector(buttonClick:)
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
2

Use Like This

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(0, 0, 100, 25);
        btn.backgroundColor = [UIColor clearColor];
        [btn setTitle:@"Play" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
        btn.center = self.center;
[self addSubview:btn];

and add your selector method

-(IBAction)btnTapped:(id)sender{

}
Vvk
  • 4,031
  • 29
  • 51
2

iOS 14.0+

you can use UIAction instead of addTarget:

            // Example: action to remove or add in the favorite list of recipes
    button.addAction(
        UIAction { _ in
            if self.isFavorite {
                self.isFavorite = false
                print("✅: Recipe is not favorite")
            } else {
                self.isFavorite = true
                print("✅ Recipe is favorite")
            }
        }, for: .touchUpInside)
YodagamaHeshan
  • 4,996
  • 2
  • 26
  • 36
Grovnyk
  • 21
  • 4
1

Swift code:

button.addTarget(self, action:"action_button", forControlEvents:.TouchUpInside)
...
func action_button() {
    //  implement me
}
superarts.org
  • 7,009
  • 1
  • 58
  • 44
1
action:@selector(buttonClick:)
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Gaurav Govilkar
  • 154
  • 1
  • 1
  • 10
0
  1. Create a function with @objc
  2. add the function as a target to the UIButton. like so:
menuButton.addTarget(self, action: #selector(menuTapped), for: .touchUpInside)
ScottyBlades
  • 12,189
  • 5
  • 77
  • 85