IVTEditMySexViewController.m 4.48 KB
//
//  IVTEditMySexViewController.m
//  IVTMeLiveTwo
//
//  Created by XRG on 16/8/24.
//  Copyright © 2016年 QLQ. All rights reserved.
//

#import "IVTEditMySexViewController.h"

@interface IVTEditMySexViewController ()
{
    UIButton *maleButton;
    UIButton *femaleButton;
    NSString *currentSelectSexString;
}
@end

@implementation IVTEditMySexViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavigationBar];

    //男
    maleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    maleButton.frame = CGRectMake(0, 0, kScreenWidth, 50);
    [maleButton setTitle:@"男" forState:UIControlStateNormal];
    [maleButton setTitleColor:UIColorFromRGBAlphaHex(0x5A5A5A, 1.0) forState:UIControlStateNormal];
    [maleButton setTitleColor:UIColorFromRGBAlphaHex(0xD31863, 1.0) forState:UIControlStateSelected];
    maleButton.backgroundColor = [UIColor whiteColor];
    [maleButton addTarget:self action:@selector(doMaleButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:maleButton];
    
    UIImageView *line = [[UIImageView alloc]init];
    line.frame = CGRectMake(10, 50, kScreenWidth-20, 0.4);
    line.alpha = 0.4;
    line.backgroundColor = UIColorFromRGBAlphaHex(0x5A5A5A, 1.0);
    [self.view addSubview:line];
    //女
    femaleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    femaleButton.frame = CGRectMake(0, 51, kScreenWidth, 50);
    [femaleButton setTitle:@"女" forState:UIControlStateNormal];
    [femaleButton setTitleColor:UIColorFromRGBAlphaHex(0x5A5A5A, 1.0) forState:UIControlStateNormal];
    [femaleButton setTitleColor:UIColorFromRGBAlphaHex(0xD31863, 1.0) forState:UIControlStateSelected];
    femaleButton.backgroundColor = [UIColor whiteColor];
    [femaleButton addTarget:self action:@selector(doFemaleButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:femaleButton];
    
    currentSelectSexString = [IVTAccountTool getSex];
    if ([currentSelectSexString length]==0 || [currentSelectSexString isEqualToString:@"unknown"]) {
        currentSelectSexString = @"female";
    }

    if ([currentSelectSexString isEqualToString:@"male"]) {
        maleButton.selected = YES;
        femaleButton.selected = NO;
    }
    else if ([currentSelectSexString isEqualToString:@"female"]){
        maleButton.selected = NO;
        femaleButton.selected = YES;
    }
    else{
        maleButton.selected = NO;
        femaleButton.selected = NO;
    }
    
}

- (void)setNavigationBar{
    self.navigationItem.title = @"性别";
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(doSave:)];
    rightBarButton.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}

- (void)doMaleButton{
    maleButton.selected = YES;
    femaleButton.selected = NO;
    currentSelectSexString = @"male";
}

- (void)doFemaleButton{
    maleButton.selected = NO;
    femaleButton.selected = YES;
    currentSelectSexString = @"female";
}

- (void)doSave:(UIBarButtonItem *)barBtnItem{
    barBtnItem.enabled = NO;

    [HUDUtil showLoading];
    [[IVTNetworkAPIClient sharedClient] modifySex:currentSelectSexString success:^(NSDictionary *successData) {
        [IVTAccountTool setSex:currentSelectSexString];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeSex" object:nil];
        if (self.editSuccessBlock) {
            self.editSuccessBlock(currentSelectSexString);
        }

        [HUDUtil showSuccess:@"修改成功"];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    } error:^(NSDictionary *successData) {
        NSString *msg = successData[@"description"];
        [HUDUtil showError:msg];

        barBtnItem.enabled = YES;

    } failure:^(NSError *failureError) {
        [HUDUtil showError:@"网络异常"];

        barBtnItem.enabled = YES;

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end