以编程方式生成UIButton并将其与IBAction相关联 [英] Programmatically generating UIButtons and associate those with IBAction
本文介绍了以编程方式生成UIButton并将其与IBAction相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果要编程生成一组按钮,然后将其与IBActions相关联,该怎么办?如果我在Interface Builder中添加按钮很容易,但是对于这种情况我不能做。
How do I do if I want to programatically generate a set of buttons and then associate those with IBActions? It's easy if I add the buttons in Interface Builder, but that I cannot do for this case.
推荐答案
按钮有方法 - (void)addTarget:(id)target action )action forControlEvents:(UIControlEvents)controlEvents
。
使用它的代码如下所示:
The code to use this would look like this:
UIButton *myButton = [[UIButton alloc] init...];
[myButton addTarget:something action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside];
这假设您的IBAction命名为 myAction
并且某些
是定义该操作的控制器。
This assumes that your IBAction is named myAction
and that something
is the controller for which that action is defined.
这篇关于以编程方式生成UIButton并将其与IBAction相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文