在不必拥有多个IBActions
的情况下,实现以下目标的最佳做法是什么?
当点击其中一个蓝色按钮时,我希望它将下面的输入字段更改为按钮指定的VALUE。
由于
答案 0 :(得分:3)
你需要为Button1设置UIButton标签,Button2 ......为0,1,依此类推
修改强>
-(IBAction)buttonAction:(UIButton *)sender
{
switch(sender) {
case 0:
//Change UITextValue
break;
case 1:
//Change UITextValue
break;
default: //Not found
break;
}
}
答案 1 :(得分:0)
U也可以通过将目标设置为相同的选择器来实现此目的
例如
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button1.frame = CGRectMake(20, 20, 100, 100);
button2.frame = CGRectMake(20, 130, 100, 100);
button1.tag = 100;
button1.backgroundColor = [UIColor greenColor];
button2.backgroundColor = [UIColor redColor];
button2.tag = 200;
[self.view addSubview:button1];
[self.view addSubview:button2];
//for selector
-(void)buttonTapped:(UIButton *)sender
{
NSLog(@"same target and tag =%d",sender.tag);
}