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

问题描述 投票:101回答:9

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

ios iphone objective-c uibutton programmatically-created
9个回答
222
投票

试试这个:

斯威夫特4

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

Objective-C的

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

您可以在Apple的文档中找到丰富的信息来源。看看UIButton's documentation,它将揭示UIButton是UIControl,的后代,它实现了add targets的方法。

--

myAction中你需要注意action:@selector(myAction)之后是否添加结肠

这是reference


23
投票

快速回答:

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

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

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


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

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 }

8
投票

试试这个:

首先在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
}

6
投票

对于Swift 3

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

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

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

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 

}

2
投票

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


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];   
© www.soinside.com 2019 - 2024. All rights reserved.