Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top