1. 程式人生 > >iOS中涉及到性別選擇的解決方式

iOS中涉及到性別選擇的解決方式

一:首先宣告一下

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

搞定收工,這裡記錄下來方便以後使用,避免重複造輪子