How to add an action for UIButton

I am new to iPhone technology. Please tell me how to add an 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]; 
+8
ios objective-c iphone ios4
source share
5 answers

You specify selectors with @selector (). So, the action parameter looks like this:

 action:@selector(buttonClick:) 
+7
source share

Use it

  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]; 
+7
source share
 action:@selector(buttonClick:) 
+1
source share

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 selection method

 -(IBAction)btnTapped:(id)sender{ } 
+1
source share

Quick code:

 button.addTarget(self, action:"action_button", forControlEvents:.TouchUpInside) ... func action_button() { // implement me } 
0
source share

All Articles