如何在xcode中以编程方式将操作添加到button中

我知道如何通过从界面生成器拖动来添加一个IBAction到一个button,但我想以编程的方式添加这个动作来节省时间,并避免不断地来回切换。 解决scheme可能非常简单,但是当我search它时,我似乎无法find任何答案。 谢谢!

尝试这个:

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

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

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

这是参考

迅速回答:

 myButton.addTarget(self, action: "click:", forControlEvents: UIControlEvents.TouchUpInside) func click(sender: UIButton) { print("click") } 
 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]; 
  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 } 

尝试这个:

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

 UIButton *btn; 

现在把它写在你的viewcontroller的.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 } 

对于Swift 3

 button.addTarget(self, action: #selector(buttonAction),for: .touchUpInside) func buttonAction(sender: UIButton!) { print("Button tapped") } 
 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 } 

UIButtoninheritance自UIControl 。 这有所有的方法来添加/删除操作: UIControl类参考 。 查看“准备和发送操作消息”部分,其中包括– sendAction:to:forEvent:– addTarget:action:forControlEvents: – sendAction:to:forEvent: – addTarget:action:forControlEvents: – sendAction:to:forEvent:

  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];