一:首先声明一下
UIButton *sexManBtn;//性别选择 男
UIButton *sexWomanBtn;//性别选择 女
UILabel *sexManLabel;
UILabel *sexWomanLabel;
BOOL sexOk;
二:初始化
sexOk = YES;
三:UI布局
sexManBtn = [UIButton buttonWithType:UIButtonTypeCustom];
sexManBtn.tag = 1000;
sexManBtn.frame = CGRectMake(125, 12, 20, cell_Height - 24);
[sexManBtn setImage:IMAGE(@"select2_b") forState:UIControlStateNormal];
[sexManBtn setImage:IMAGE(@"select2_a") forState:UIControlStateSelected];
[sexManBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
sexManLabel = [[UILabel alloc] init];
sexManLabel.frame = CGRectMake(150, 5, 20, cell_Height - 12);
sexManLabel.backgroundColor = [UIColor clearColor];
sexManLabel.textAlignment = 0;
sexManLabel.textColor = [UIColor blackColor];
sexManLabel.font = [UIFont systemFontOfSize:14];
sexManLabel.text = @"男";
sexWomanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
sexWomanBtn.tag = 1001;
sexWomanBtn.frame = CGRectMake(180, 12, 20, cell_Height - 24);
[sexWomanBtn setImage:IMAGE(@"select2_b") forState:UIControlStateNormal];
[sexWomanBtn setImage:IMAGE(@"select2_a") forState:UIControlStateSelected];
[sexWomanBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
sexWomanLabel = [[UILabel alloc] init];
sexWomanLabel.frame = CGRectMake(205, 5, 20, cell_Height - 12);
sexWomanLabel.backgroundColor = [UIColor clearColor];
sexWomanLabel.textAlignment = 0;
sexWomanLabel.textColor = [UIColor blackColor];
sexWomanLabel.font = [UIFont systemFontOfSize:14];
sexWomanLabel.text = @"女";
sexLabel.text = titleArray[indexPath.row];
[cell.contentView addSubview:sexLabel];
[cell.contentView addSubview:sexManBtn];
[cell.contentView addSubview:sexManLabel];
[cell.contentView addSubview:sexWomanBtn];
[cell.contentView addSubview:sexWomanLabel];
// 我这里是加在tableView中的
if (sexOk) {
sexManBtn.selected = YES;
}else {
sexWomanBtn.selected = YES;
}
四:点击按钮之后的判断
case 1000:
{
DebugLog(@"男");
sexManBtn.selected = !sexManBtn.selected;
sexWomanBtn.selected = !sexWomanBtn.selected;
if (sexManBtn.selected) {
sexOk = YES; //标志性别选择为男
}else {
sexOk = NO;
}
DebugLog(@"性别选择%id",sexOk);
}
break;
case 1001:
{
DebugLog(@"女");
sexManBtn.selected = !sexManBtn.selected;
sexWomanBtn.selected = !sexWomanBtn.selected;
if (sexManBtn.selected) {
sexOk = YES;
}else {
sexOk = NO;
}
DebugLog(@"性别选择%id",sexOk);
}
break;
六:上送服务端
if (sexOk) {
[PostInfos setObject:@"M" forKey:@"Sex"];//性别 男
} else {
[PostInfos setObject:@"F" forKey:@"Sex"];//性别 女
}
搞定收工,这里记录下来方便以后使用,避免重复造轮子