Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set a block as a target on a UIButton?

I need to send extra arguments to a method on button press, but can't find a way to make addTarget send a selector to do that.

I'd rather use a block anyway, but is there a method for attaching a block to a UIButton at run time?

like image 481
James Avatar asked Dec 03 '11 01:12

James


2 Answers

Yes, you can do it easily with BlocksKit library:

#import <BlocksKit/UIControl+BlocksKit.h>

[someButton addEventHandler:^(id sender) {
    // some action here
} forControlEvents:(UIControlEventTouchUpInside)];

or use RxSwift button.rx.tap observable.

like image 114
Leszek Zarna Avatar answered Sep 30 '22 20:09

Leszek Zarna


Not by default. You could subclass UIButton to make this happen, though. Someone did it here.

like image 44
Michael Frederick Avatar answered Sep 30 '22 18:09

Michael Frederick