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

#import "IVTEditMyNickNameViewController.h"

@interface IVTEditMyNickNameViewController ()
{
    UITextField *signTextField;
    NSString *textString;
}
@end

@implementation IVTEditMyNickNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavigationBar];
    
    UIView *backGround = [[UIView alloc]initWithFrame:CGRectMake(0, 5, kScreenWidth, 46)];
    backGround.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backGround];
    
    signTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 20, 46)];
    signTextField.backgroundColor = [UIColor whiteColor];
    signTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    signTextField.font = [UIFont IVTH3Font];
    signTextField.textColor = [UIColor IVTMainTextColor];
    signTextField.text = [[IVTAccountTool getNickName] base64DecodedToString];
    [backGround addSubview:signTextField];
    [signTextField becomeFirstResponder];
    
    textString = signTextField.text;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification 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 (signTextField.text.length > 14) {
        signTextField.text = textString;
    }
    else{
        textString = signTextField.text;
    }
}

- (void)doSave:(UIBarButtonItem *)barBtnItem{
    if (signTextField.text.length > 0) {
        barBtnItem.enabled = NO;
        [HUDUtil showLoading];
        [[IVTNetworkAPIClient sharedClient] modifyNickName:[signTextField.text base64EncodedFromString] success:^(NSDictionary *successData) {
            [IVTAccountTool setNickName:[signTextField.text base64EncodedFromString]];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"successToChangeNickName" object:nil];
            if (self.editSuccessBlock) {
                self.editSuccessBlock(signTextField.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 showInfo:@"昵称不能为空"];
    }
}

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