static 关键字,只初始化一次如果程序运行中没有改变它的值的话会一直根据程序变化
NSArray *array=@[@"车",@"马",@"象",@"王",@"后",@"象",@"马",@"车"];
for (int i=0; i<8; i++) {
for (int j=0; j<8; j++) {
//判断为偶数行
if (i%2==0) {
if (j%2==0) {
//创建黑颜色的UILabel
UIButton *blackLabel=[UIButton buttonWithType:UIButtonTypeSystem];
[blackLabel addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
blackLabel.frame=CGRectMake(j*40, i*40+20, 40, 40);
blackLabel.backgroundColor=[UIColor blackColor];
if (i==0) {
//设置文字显示的颜色为红
//blackLabel.textColor=[UIColor redColor];
[blackLabel setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//blackLabel.text=[array objectAtIndex:j];
[blackLabel setTitle:[array objectAtIndex:j] forState:UIControlStateNormal];
// blackLabel.textAlignment=NSTextAlignmentCenter;
}
if(i==6)
{
//blackLabel.text=@"兵";
[blackLabel setTitle:@"兵" forState:UIControlStateNormal];
//blackLabel.textColor=[UIColor greenColor];
[blackLabel setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
}
[self.window addSubview:blackLabel];
}
//如果为奇数列
else
{
UIButton *whiteLable=[UIButton buttonWithType:UIButtonTypeSystem];
[whiteLable addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
whiteLable.frame=CGRectMake(j*40, i*40+20, 40, 40);
whiteLable.backgroundColor=[UIColor yellowColor];
if (i==0) {
//whiteLable.text=[array objectAtIndex:j];
[whiteLable setTitle:[array objectAtIndex:j] forState:UIControlStateNormal];
//whiteLable.textColor=[UIColor redColor];
[whiteLable setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//whiteLable.textAlignment=NSTextAlignmentCenter;
}
if(i==6)
{
//whiteLable.text=@"兵";
[whiteLable setTitle:@"兵" forState:UIControlStateNormal];
//whiteLable.textColor=[UIColor greenColor];
[whiteLable setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
}
[self.window addSubview:whiteLable];
}
}
//如果为奇数行
else
{
//如果为奇数列,应为黑颜色
if(j%2==1)
{
//创建黑颜色的UILabel
UIButton *blackLabel=[UIButton buttonWithType:UIButtonTypeSystem];
[blackLabel addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
blackLabel.frame=CGRectMake(j*40, i*40+20, 40, 40);
blackLabel.backgroundColor=[UIColor blackColor];
[self.window addSubview:blackLabel];
if (i==1) {
//blackLabel.textAlignment=NSTextAlignmentCenter;
//blackLabel.text=@"兵";
[blackLabel setTitle:@"兵" forState:UIControlStateNormal];
//blackLabel.textColor=[UIColor redColor];
[blackLabel setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
if (i==7) {
//blackLabel.textColor=[UIColor greenColor];
[blackLabel setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
//blackLabel.text=[array objectAtIndex:j];
[blackLabel setTitle:[array objectAtIndex:j] forState:UIControlStateNormal];
}
}
//如果为偶数列
else
{
//UILabel *whiteLable=[[UILabel alloc]initWithFrame:CGRectMake(j*40, i*40+20, 40, 40)];
UIButton *whiteLable=[UIButton buttonWithType:UIButtonTypeSystem];
[whiteLable addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
whiteLable.frame=CGRectMake(j*40, i*40+20, 40, 40);
whiteLable.backgroundColor=[UIColor yellowColor];
[self.window addSubview:whiteLable];
if (i==1) {
// whiteLable.textAlignment=NSTextAlignmentCenter;
//whiteLable.text=@"兵";
[whiteLable setTitle:@"兵" forState:UIControlStateNormal];
//whiteLable.textColor=[UIColor redColor];
[whiteLable setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
if (i==7) {
//whiteLable.textColor=[UIColor greenColor];
[whiteLable setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
//whiteLable.text=[array objectAtIndex:j];
[whiteLable setTitle:[array objectAtIndex:j] forState:UIControlStateNormal];
}
}
}
}
}
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
-(void)btnClick:(UIButton *)bt
{
static int count=0;
static UIButton *btn=nil;
if (count==0) {
//btn保存第一点击时的按钮对象
btn=bt;
count++;
}
else
{
NSString *temp;
UIColor *tempColor;
temp=bt.currentTitle;
//获得按钮上的文本颜色
tempColor=bt.titleLabel.textColor;
[bt setTitle:btn.currentTitle forState:UIControlStateNormal];
[bt setTitleColor:btn.titleLabel.textColor forState:UIControlStateNormal];
[btn setTitle:temp forState:UIControlStateNormal];
[btn setTitleColor:tempColor forState:UIControlStateNormal];
count=0;
}
}