如果我正确理解您的问题,您可以在头文件中声明您的按钮,如下所示:
@interface myController : UIViewController
{
UIButton *b1;
UIButton *b2;
}
m-file 中的 tmen(在 viewDidLoad 中)您可以使用一个选择器和不同的标签设置此按钮:(有关创建按钮的更多信息:如何以编程方式创建基本 UIButton?)
-(void)viewDidLoad
{
[super viewDidLoad];
b1 = [UIButton buttonwithType:UIButtonTypeCustom];
[b1 addTarget:self withAction:@selector(clickINMyButtons:) forState:UIControlTouchUPInside]; // sorry, I don't remember correct syntax, i'll correct this some later if you needed in it.
b1.tag = 1;
b1.frame = CGRectMake(0,0,12,12); //example
[self.view addSubView:b1];
}
同样声明 b2 不同:
b2.tag = 2;
所以,然后你用改变不透明度来实现你的选择器:
-(void)clickINMyButtons:(UIButton *)sender
{
if (sender.tag == 1)
{
sender.alpha = 1; // or b1.alpha = 1;
b2.alpha = 0.2;
}
else if (sender.tag == 2)
{
sender.alpha = 1; // or b2.alpha = 1;
b1.alpha = 0.2;
}
}