如何在xcode中以编程方式向按钮添加动作 [英] How do you add an action to a button programmatically in xcode
问题描述
我知道如何通过从界面构建器拖动来向按钮添加IBAction,但我想以编程方式添加动作以节省时间并避免不断地来回切换。解决方案可能非常简单,但我在搜索时似乎无法找到任何答案。谢谢!
I know how to add an IBAction to a button by dragging from the interface builder, but I want to add the action programmatically to save time and to avoid switching back and forth constantly. The solution is probably really simple, but I just can't seem to find any answers when I search it. Thank you!
推荐答案
试试这个:
[myButton addTarget:self
action:@selector(myAction)
forControlEvents:UIControlEventTouchUpInside];
您可以在Apple的文档中找到丰富的信息来源。请查看 UIButton的文档,它将揭示UIButton是 UIControl,实现添加目标。
You can find a rich source of information in Apple's Documentation. Have a look at the UIButton's documentation, it will reveal that UIButton is a descendant of UIControl, which implements the method to add targets.
-
你需要注意在
中 myAction
之后是否添加冒号操作:@selector(myAction)
You'll need to pay attention to whether add colon or not after myAction
in
action:@selector(myAction)
这是参考
这篇关于如何在xcode中以编程方式向按钮添加动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!