iOS中涉及到性别选择的解决方式

本文档详细介绍了在iOS应用中实现性别选择的功能,包括声明、初始化、UI布局、按钮点击后的判断逻辑以及如何将选择的性别数据上传到服务端。通过此记录,可以为后续开发提供便利,减少重复工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:首先声明一下

	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"];//性别 女
    }

搞定收工,这里记录下来方便以后使用,避免重复造轮子

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值