简体   繁体   中英

How to trigger UIButton action programmatically

I have several buttons in one class and their actions are separate. I created the instance of that class in another class, and I have the UIButton array in the 2nd class. I want to call each button's action programmatically. Is there any way to do this in iOS?

UIButton有一种方法可以调用链接到某个控件事件的目标/选择器:

[button sendActionsForControlEvents:UIControlEventTouchUpInside];

斯威夫特3.0 / 4.0 / 5.0

button.sendActions(for: .touchUpInside)

You can simply call first class's action method as instance method from other classes by passing a UIbutton id

eg: in 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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM