标签:
//可直接复制 测试
#import "ViewController.h"
@interface ViewController ()
{
NSInteger num;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
num = -1 ;
[self layoutView];
}
- (void)layoutView
{
int totalloc=3;
int count = 6;
CGFloat appvieww=100;
CGFloat appviewh=100;
CGFloat margin=(self.view.frame.size.width -totalloc*appvieww)/(totalloc+1);
for (int i=0; i<count; i++) {
int row=i/totalloc;
int loc=i%totalloc;
CGFloat appviewx=margin+(margin+appvieww)*loc;
CGFloat appviewy=margin+(margin+appviewh)*row;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(appviewx, appviewy, appvieww, appviewh);
[button setTitle:@"按钮" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
button.tag = i +1;
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
- (void)click:(UIButton *)button
{
UIButton *btn1 = (UIButton *)[self.view viewWithTag:num];
if (num != 0)
{
btn1.selected = NO;
}
num = button.tag;
button.selected = YES;
}
@end
@end
保持按钮选中状态,当点击另一个按钮后之前被选中的按钮恢复原样 实现代码
标签:
原文地址:http://www.cnblogs.com/qimingtainanle/p/4874711.html