3

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Csabi
  • 3,097
  • 17
  • 59
  • 107

1 Answers1

4

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.

Vladimir
  • 170,431
  • 36
  • 387
  • 313