我想实现一个功能,当我点击"operatorButton"的titleColor改变,但当我点击其他按钮除了"operatorButton", "operatorButton"的titleColor回到原色。
这里我有一个问题:我在"viewDidLoad"方法中声明的"operatorButton"是局部变量,我不能在其他方法中访问。我如何声明"operatorButton"作为一个全局变量,代表每一个项目在NSArray"operationButton"?
#import <UIKit/UIKit.h>
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *operationButton;
- (void)viewDidLoad {
[super viewDidLoad];
for (UIButton *operatorButton in self.operationButton){
[operatorButton setTitleColor:[UIColor colorWithRed:248.0/255 green:148.0/255 blue:52.0/255 alpha:1.0]
forState:UIControlStateSelected];
[operatorButton setTitleColor: [UIColor colorWithRed:95.0/255 green:105.0/255 blue:114.0/255 alpha:1.0]
forState:UIControlStateNormal];
}
}
您已经使用IBOutletCollection将其声明为数组。你可以访问self。因为它是一个属性。
为所有按钮创建一个IBAction
,然后尝试以下操作:
-(IBAction)selectedButton:(id)sender
{
UIButton *senderButton = (UIButton *)sender;
for (UIButton *operatorButton in self.operationButton)
{
if ([operatorButton isEqualTo:senderButton])
{
//set title color to your selected Color
}
else
{
//set title color to original color on buttons not clicked
}
}
}