Bind a function to UIButton programmatically

How can I bind a function to my button that 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 the same thing as IB when I associate a function with a button. Is it possible?

+3
source share
1 answer

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

+4
source

Source: https://habr.com/ru/post/1785438/


All Articles