UIButton
我需要在 iOS 中将几个按钮 ( ) 组合为一个。如果单击任一按钮,则应使用按钮 id 调用单个函数,然后调用相应的处理程序。
我如何在 iOS 中做到这一点?我的研究表明这UIView
可能是答案。但是如何在印刷机上自动调用相同的功能?
是的,只需将它们全部绑定到同一个 IBAction,它应该可以正常工作。因此,在您的标题中创建 -(IBAction) buttonPushed: 然后在 IB 中将所有按钮连接到相同的操作。
-(void)buttonAction:(id)sender {
UIButton *btn = (UIButton*)sender; //We know sender will be a UIButton
switch (btn.tag) {
case 1: { /* do stuff for first button */ break; }
case 2: { /* do stuff for second button */ break; }
case 3: { /* do stuff for third button */ break; }
//Etc.
}
}
-(IBAction) buttonClicked:
在 viewcontroll.h 文件中创建 IBAction 并将所有按钮与此操作连接起来。
您可以像这样添加多个按钮。
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 40, 50, 50);
[btn setTitle:@"Send" forState:UIControlStateNormal];
[btn setTag:1];
[btn addTarget:self action:@selector(sendBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 100, 50, 50);
[btn1 setTitle:@"YES" forState:UIControlStateNormal];
[btn1 setTag:2];
[btn1 addTarget:self action:@selector(sendBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
-(IBAction)sendBtnPressed:(id)sender
{
int tag = [(UIButton *) sender tag];
if(tag == 1)
{
}
else if (tag == 2)
{
}
}