我知道如何通过从界面构建器中拖动来将IBAction添加到按钮,但是我想以编程方式添加该动作以节省时间并避免不断地来回切换。该解决方案可能真的很简单,但是我在搜索时似乎找不到任何答案。谢谢!
我知道如何通过从界面构建器中拖动来将IBAction添加到按钮,但是我想以编程方式添加该动作以节省时间并避免不断地来回切换。该解决方案可能真的很简单,但是我在搜索时似乎找不到任何答案。谢谢!
Answers:
试试这个:
斯威夫特4
myButton.addTarget(self,
action: #selector(myAction),
for: .touchUpInside)
目标C
[myButton addTarget:self
action:@selector(myAction)
forControlEvents:UIControlEventTouchUpInside];
您可以在Apple文档中找到丰富的信息来源。看一下UIButton的文档,它将发现UIButton是UIControl的后代,该控件实现了添加目标的方法。
-
你需要注意是否加冒号或之后不myAction
中
action:@selector(myAction)
这是参考。
快速答案:
myButton.addTarget(self, action: "click:", for: .touchUpInside)
func click(sender: UIButton) {
print("click")
}
说明文件:https : //developer.apple.com/documentation/uikit/uicontrol/1618259-addtarget
:
在函数参数中有一个占位符。因为click
采用sender
参数,所以:
在选择器字符串中将其替换为。
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];
试试这个:
首先将其写入您的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
}
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 }
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
}
UIButton
继承自UIControl
。它具有添加/删除动作的所有方法:UIControl类参考。查看“准备和发送操作消息”部分,其中包括– sendAction:to:forEvent:
和– addTarget:action:forControlEvents:
。
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];