How to trigger UIButton action programmatically

I have multiple buttons in the same class and their actions are separate. I created an instance of this class in another class and I have a UIButton array in the second class. I want to call each button programmatically. Is there a way to do this in iOS?

+7


source to share


3 answers


UIButton has a method to call target / selectors associated with a specific control event:



[button sendActionsForControlEvents:UIControlEventTouchUpInside];

+23


source


Swift 3.0 / 4.0 / 5.0



button.sendActions(for: .touchUpInside)

      

+12


source


You can just call the action method of the first class as an instance method from other classes by passing the UIbutton id

eg: in the first class " ClassA "

- (IBAction)classAbuttonAction1:(id)sender;
- (IBAction)classAbuttonAction2:(id)sender;

      

from another class

UIButton *bClassButton = [[UIButton alloc]init];    
ClassA *instance = [[ClassA alloc]init];

[instance classAbuttonAction1:bClassButton];
[instance classAbuttonAction2:bClassButton];

      

0


source







All Articles