ios 如何在 xcode 中以编程方式向按钮添加操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5843427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:47:12  来源:igfitidea点击:

How do you add an action to a button programmatically in xcode

iosiphoneobjective-cuibuttonprogrammatically-created

提问by aggiesfan64

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!

我知道如何通过从界面构建器中拖动来将 IBAction 添加到按钮,但我想以编程方式添加操作以节省时间并避免不断来回切换。解决方案可能非常简单,但我在搜索时似乎找不到任何答案。谢谢!

回答by Nick Weaver

Try this:

尝试这个:

Swift 4

斯威夫特 4

myButton.addTarget(self,
                   action: #selector(myAction),
                   for: .touchUpInside)

Objective-C

目标-C

[myButton addTarget:self 
             action:@selector(myAction) 
   forControlEvents:UIControlEventTouchUpInside];

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.

您可以在 Apple 的文档中找到丰富的信息来源。看看UIButton 的文档,它会发现 UIButton 是UIControl的后代它实现了添加目标的方法。

--

——

You'll need to pay attention to whether add colon or not after myActionin action:@selector(myAction)

您需要注意在myActionin 之后是否添加冒号action:@selector(myAction)

Here's the reference.

这是参考

回答by Glauco Neves

Swift answer:

迅捷回答:

myButton.addTarget(self, action: "click:", for: .touchUpInside)

func click(sender: UIButton) {
    print("click")
}

Documentation: https://developer.apple.com/documentation/uikit/uicontrol/1618259-addtarget

文档:https: //developer.apple.com/documentation/uikit/uicontrol/1618259-addtarget

回答by Developer

CGRect buttonFrame = CGRectMake( 10, 80, 100, 30 );
        UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
        [button setTitle: @"My Button" forState: UIControlStateNormal];
        [button addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[view addSubview:button];

回答by krushnsinh

try this:

尝试这个:

first write this in your .h file of viewcontroller

首先将其写入您的 viewcontroller .h 文件中

UIButton *btn;

Now write this in your .m file of viewcontrollers viewDidLoad.

现在将其写入您的视图控制器 viewDidLoad 的 .m 文件中。

btn=[[UIButton alloc]initWithFrame:CGRectMake(50, 20, 30, 30)];
[btn setBackgroundColor:[UIColor orangeColor]];
//adding action programatically
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

write this outside viewDidLoad method in .m file of your view controller

将此外部 viewDidLoad 方法写入视图控制器的 .m 文件中

- (IBAction)btnClicked:(id)sender
{
   //Write a code you want to execute on buttons click event
}

回答by iOSDeveloper

 CGRect buttonFrame = CGRectMake( x-pos, y-pos, width, height );   //
 CGRectMake(10,5,10,10) 

 UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];

 button setTitle: @"My Button" forState: UIControlStateNormal];

 [button addTarget:self action:@selector(btnClicked:) 
 forControlEvents:UIControlEventTouchUpInside];

 [button setTitleColor: [UIColor BlueVolor] forState:
 UIControlStateNormal];

 [view addSubview:button];




 -(void)btnClicked {
    // your code }

回答by SanRam

For Swift 3

对于 Swift 3

Create a function for button action first and then add the function to your button target

首先为按钮操作创建一个函数,然后将该函数添加到您的按钮目标

func buttonAction(sender: UIButton!) {
    print("Button tapped")
}

button.addTarget(self, action: #selector(buttonAction),for: .touchUpInside)

回答by user2677311

UIButton *btnNotification=[UIButton buttonWithType:UIButtonTypeCustom];
btnNotification.frame=CGRectMake(130,80,120,40);
btnNotification.backgroundColor=[UIColor greenColor];
[btnNotification setTitle:@"create Notification" forState:UIControlStateNormal];
[btnNotification addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnNotification];


}

-(void)btnClicked
{
    // Here You can write functionality 

}

回答by odrm

UIButtoninherits from UIControl. That has all of the methods to add/remove actions: UIControl Class Reference. Look at the section "Preparing and Sending Action Messages", which covers – sendAction:to:forEvent:and – addTarget:action:forControlEvents:.

UIButton继承自UIControl. 它具有添加/删除操作的所有方法:UIControl Class Reference。查看“准备和发送操作消息”部分,其中包括– sendAction:to:forEvent:– addTarget:action:forControlEvents:

回答by Arjun Sa

 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
       action:@selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];