// // YOUPAILCUpdateNickVC.m // LiveChat // // Created by 张灿 on 2018/4/8. // Copyright © 2018年 DaTongIntelligent. All rights reserved. // #import "YOUPAILCUpdateNickVC.h" @interface YOUPAILCUpdateNickVC () @property(nonatomic,strong)UITextField* youpaiptextField; /* 清除按钮 */ //@property (strong, nonatomic) UIButton *clearButton; @end @implementation YOUPAILCUpdateNickVC - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; // self.sx_disableInteractivePop = NO; } - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; // [self.textField becomeFirstResponder]; self.sx_disableInteractivePop = YES; } - (void)viewDidLoad { [super viewDidLoad]; [self youpaifsetupView]; self.view.backgroundColor = [UIColor redColor]; self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifbackNeedSave) image:[UIImage imageNamed:@"vqu_images_navigation_back_black"]]; } - (void)youpaifbackNeedSave{ if ([self.youpaiptextField.text isEqualToString:self.youpaipnickName] && (![self.youpaiptextField.text isEqualToString:@""])) { [self.navigationController popViewControllerAnimated:YES]; }else{ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"保存信息" message:@"是否保存本次修改内容?" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"不保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ [self.navigationController popViewControllerAnimated:YES]; }]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ [self youpaifSaveRemark]; }]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil]; } } - (void)youpaifsetupView{ //设置导航栏 self.title = @"昵称"; self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifSaveRemark) title:@"保存" font:LCFont15 titleColor:LZA3AABEColor highlightedColor:LZA3AABEColor titleEdgeInsets:UIEdgeInsetsZero]; self.navigationItem.rightBarButtonItem.enabled = NO; //设置textView UIScrollView* scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, NavBarHeight, KScreenWidth, KScreenHeight-NavBarHeight)]; scrollView.contentSize = CGSizeMake(KScreenWidth, KScreenHeight-NavBarHeight+1); scrollView.bounces = YES; scrollView.showsHorizontalScrollIndicator = NO; scrollView.showsVerticalScrollIndicator = NO; [self.view addSubview:scrollView]; //bgView UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(14,25, KScreenWidth-28,49)]; [scrollView addSubview:bgView]; bgView.backgroundColor = LZF7F8FAColor; bgView.layer.cornerRadius = 6.0f; bgView.layer.masksToBounds = YES; UITextField* textField = [[UITextField alloc]initWithFrame:CGRectMake(0,0, KScreenWidth-28,49)]; textField.tintColor = LZ7C69FEColor; textField.backgroundColor = LZF7F8FAColor; textField.layer.cornerRadius = 6.0f; textField.layer.masksToBounds = YES; textField.delegate = self; textField.text = self.youpaipnickName; textField.font = LCFont(14); textField.textColor = LZ273145Color; textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,12,49)]; textField.leftViewMode = UITextFieldViewModeAlways; textField.clearButtonMode = UITextFieldViewModeAlways; [[textField valueForKey:@"_clearButton"] setImage:[UIImage imageNamed:@"vqu_images_D_nick_clean"] forState:UIControlStateNormal]; NSString *holderText = @"输入昵称"; NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText]; [placeholder addAttribute:NSForegroundColorAttributeName value:LZD3D1D7Color range:NSMakeRange(0, holderText.length)]; [placeholder addAttribute:NSFontAttributeName value:LCFont(14) range:NSMakeRange(0, holderText.length)]; textField.attributedPlaceholder = placeholder; self.youpaiptextField = textField; [bgView addSubview:textField]; // UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom]; // clearButton.frame = CGRectMake(KScreenWidth-28-20-10, 29/2, 20, 20); // [clearButton addTarget:self action:@selector(clearButtonDidClick:) forControlEvents:UIControlEventTouchUpInside]; // [clearButton setImage:[UIImage imageNamed:@"vqu_images_D_nick_clean"] forState:UIControlStateNormal]; // [bgView addSubview:clearButton]; // self.clearButton = clearButton; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youpaiftextDidChange) name:UITextFieldTextDidChangeNotification object:textField]; } //- (void)clearButtonDidClick: (UIButton *)button { // self.textField.text = nil; // } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [self.youpaiptextField resignFirstResponder]; return YES; } - (void)youpaiftextDidChange{ NSLog(@"%@",self.youpaiptextField.text); if ([self.youpaiptextField.text isEqualToString:self.youpaipnickName]) { self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifSaveRemark) title:@"保存" font:LCFont15 titleColor:LZA3AABEColor highlightedColor:LZA3AABEColor titleEdgeInsets:UIEdgeInsetsZero]; self.navigationItem.rightBarButtonItem.enabled = NO; }else{ self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifSaveRemark) title:@"保存" font:LCFont15 titleColor:LZ7C69FEColor highlightedColor:LZ7C69FEColor titleEdgeInsets:UIEdgeInsetsZero]; self.navigationItem.rightBarButtonItem.enabled = YES; } if(self.youpaiptextField.text.length>10){ self.youpaiptextField.text = [self.youpaiptextField.text substringToIndex:10]; } } - (void)youpaifSaveRemark{ //加一个正则判断昵称是否符合规则 正则限制/[\x{4e00}-\x{9fa5}\w]+$/u 长度限制2~12个utf8字符 if (self.youpaiptextField.text.length<1 || self.youpaiptextField.text.length>10) { [ZCHUDHelper showTitle:@"昵称长度为1~10个字符"]; }else{ [LCHttpHelper requestWithURLString:UpdateUserMsg parameters:@{@"nickname":self.youpaiptextField.text} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) { NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code==0) {//成功 YOUPAILCUserModel* userModel = [LCSaveModel getUserModel]; userModel.youpaipuserinfo.youpaipnickname = self.youpaiptextField.text; [LCSaveModel saveUserModel:userModel]; if (self.youpaipblock) { self.youpaipblock(self.youpaiptextField.text); } [self.navigationController popViewControllerAnimated:YES]; } } failure:^(NSError *error) { }]; } } - (void)dealloc { [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:self.youpaiptextField]; } @end