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

#import "IVTEditMyProfessionalViewController.h"

@interface IVTEditMyProfessionalViewController ()
{
    UITextView *professionalTextView;
    NSString *textString;
    UILabel *numLable;
}
@end

@implementation IVTEditMyProfessionalViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavigationBar];

    professionalTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, 5, kScreenWidth, 92)];
    professionalTextView.font = [UIFont IVTH3Font];
    professionalTextView.textColor = [UIColor IVTMainTextColor];
    professionalTextView.returnKeyType  = UIReturnKeyDone;
    professionalTextView.clearsOnInsertion = YES;
    professionalTextView.autocorrectionType = UITextAutocorrectionTypeNo;//不联想输入
    professionalTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;//不大小写
    professionalTextView.text = [[IVTAccountTool getProfessional] base64DecodedToString];
    [self.view addSubview:professionalTextView];
    [professionalTextView becomeFirstResponder];

    textString = professionalTextView.text;
    
    CGFloat x  = professionalTextView.bounds.size.width-25;
    CGFloat y  = professionalTextView.bounds.size.height-25;
    numLable = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 20, 20)];
    numLable.text = @"14";
    numLable.textColor = [UIColor IVTAssistColor];
    numLable.font = [UIFont YXFontOfSize:14];
    [professionalTextView addSubview:numLable];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextViewTextDidChangeNotification object:nil];
}

- (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)textFieldDidChange:(NSNotification *)notification{
    if (professionalTextView.text.length > 14) {
        professionalTextView.text = textString;
    }
    else{
        textString = professionalTextView.text;
        numLable.text = [NSString stringWithFormat:@"%ld",14-professionalTextView.text.length];
    }
}

- (void)doSave:(UIBarButtonItem *)barBtnItem{
    if (professionalTextView.text.length > 0) {
        barBtnItem.enabled = NO;
        [HUDUtil showLoading];

        [[IVTNetworkAPIClient sharedClient] modifyProfessional:[professionalTextView.text base64EncodedFromString] success:^(NSDictionary *successData) {
            [IVTAccountTool setProfessional:[professionalTextView.text base64EncodedFromString]];
            if (self.editSuccessBlock) {
                self.editSuccessBlock(professionalTextView.text);
            }
            [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;
        }];
    }
    else{
        [HUDUtil showError:@"职业不能为空"];
    }
}

- (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