Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anchor a function to UIButton programmatically

How can I anchor a function to my Button which I created this way

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.font            = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode   = UILineBreakModeTailTruncation;
button.titleLabel.shadowOffset    = CGSizeMake (1.0, 0.0);
[button addTarget:self                     
               action:@selector(aMethod:)
     forControlEvents:UIControlEventTouchDown];

button.frame = CGRectMake(80.0, 160.0, 160.0, 40.0);

[button setTitle:string forState:UIControlStateNormal]; 
[self.view addSubview:button];

I want to do similar as it do IB when I link a function to a button. Is it possible?

like image 809
Csabi Avatar asked Jul 25 '26 19:07

Csabi


1 Answers

You're doing it right (assuming your aMethod gets 1 parameter - the ui control that triggers event). The only difference from action wired by default in IB - is that in IB action is set for UIControlEventTouchUpInside event.

like image 169
Vladimir Avatar answered Jul 27 '26 08:07

Vladimir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!