在不必拥有多个 的情况下实现以下目标的最佳做法是IBActions
什么?
单击其中一个蓝色按钮时,我希望它将下面的输入字段更改为按钮分配的值。
谢谢
您需要将 Button1、Button2... 的 UIButton 标签设置为 0、1 等
编辑
-(IBAction)buttonAction:(UIButton *)sender
{
switch(sender) {
case 0:
//Change UITextValue
break;
case 1:
//Change UITextValue
break;
default: //Not found
break;
}
}
你也可以通过将目标设置为相同的选择器来实现这一点,例如
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);
}