108

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

4

10 回答 10

227

尝试这个:

斯威夫特 4

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

Objective-C

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

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

--

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

这是参考

于 2011-04-30T17:26:51.927 回答
24

快速回答:

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

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

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

于 2015-06-25T19:31:29.927 回答
14
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];
于 2012-02-11T05:59:56.300 回答
9

尝试这个:

首先把它写在你的 viewcontroller .h 文件中

UIButton *btn;

现在把它写在你的 viewcontrollers 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];

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

- (IBAction)btnClicked:(id)sender
{
   //Write a code you want to execute on buttons click event
}
于 2015-04-30T06:17:47.890 回答
8
 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 }
于 2014-04-16T10:19:10.200 回答
6

对于斯威夫特 3

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

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

button.addTarget(self, action: #selector(buttonAction),for: .touchUpInside)
于 2016-12-24T16:13:17.000 回答
3
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 

}
于 2013-10-07T14:18:04.560 回答
2

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

于 2011-04-30T17:27:04.403 回答
1
 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];   
于 2014-04-16T09:07:54.457 回答
0

IOS 14 SDK:您可以添加带有关闭回调的操作:

let button = UIButton(type: .system, primaryAction: UIAction(title: "Button Title", handler: { _ in
            print("Button tapped!")
        }))

获取对控制发送者的引用

let textField = UITextField()
textField.addAction(UIAction(title: "", handler: { action in
    let textField = action.sender as! UITextField
    print("Text is \(textField.text)")
}), for: .editingChanged)

资源

于 2020-09-27T09:57:45.327 回答