YOUPAILCUpdateNickVC.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // YOUPAILCUpdateNickVC.m
  3. // LiveChat
  4. //
  5. // Created by 张灿 on 2018/4/8.
  6. // Copyright © 2018年 DaTongIntelligent. All rights reserved.
  7. //
  8. #import "YOUPAILCUpdateNickVC.h"
  9. @interface YOUPAILCUpdateNickVC ()<UITextFieldDelegate>
  10. @property(nonatomic,strong)UITextField* youpaiptextField;
  11. /* 清除按钮 */
  12. //@property (strong, nonatomic) UIButton *clearButton;
  13. @end
  14. @implementation YOUPAILCUpdateNickVC
  15. - (void)viewWillDisappear:(BOOL)animated{
  16. [super viewWillDisappear:animated];
  17. // self.sx_disableInteractivePop = NO;
  18. }
  19. - (void)viewDidAppear:(BOOL)animated{
  20. [super viewDidAppear:animated];
  21. // [self.textField becomeFirstResponder];
  22. self.sx_disableInteractivePop = YES;
  23. }
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. [self youpaifsetupView];
  27. self.view.backgroundColor = [UIColor redColor];
  28. self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifbackNeedSave) image:[UIImage imageNamed:@"vqu_images_navigation_back_black"]];
  29. }
  30. - (void)youpaifbackNeedSave{
  31. if ([self.youpaiptextField.text isEqualToString:self.youpaipnickName] && (![self.youpaiptextField.text isEqualToString:@""])) {
  32. [self.navigationController popViewControllerAnimated:YES];
  33. }else{
  34. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"保存信息" message:@"是否保存本次修改内容?" preferredStyle:UIAlertControllerStyleAlert];
  35. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"不保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  36. [self.navigationController popViewControllerAnimated:YES];
  37. }];
  38. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  39. [self youpaifSaveRemark];
  40. }];
  41. [alertController addAction:cancelAction];
  42. [alertController addAction:okAction];
  43. [self presentViewController:alertController animated:YES completion:nil];
  44. }
  45. }
  46. - (void)youpaifsetupView{
  47. //设置导航栏
  48. self.title = @"昵称";
  49. self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifSaveRemark) title:@"保存" font:LCFont15 titleColor:LZA3AABEColor highlightedColor:LZA3AABEColor titleEdgeInsets:UIEdgeInsetsZero];
  50. self.navigationItem.rightBarButtonItem.enabled = NO;
  51. //设置textView
  52. UIScrollView* scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, NavBarHeight, KScreenWidth, KScreenHeight-NavBarHeight)];
  53. scrollView.contentSize = CGSizeMake(KScreenWidth, KScreenHeight-NavBarHeight+1);
  54. scrollView.bounces = YES;
  55. scrollView.showsHorizontalScrollIndicator = NO;
  56. scrollView.showsVerticalScrollIndicator = NO;
  57. [self.view addSubview:scrollView];
  58. //bgView
  59. UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(14,25, KScreenWidth-28,49)];
  60. [scrollView addSubview:bgView];
  61. bgView.backgroundColor = LZF7F8FAColor;
  62. bgView.layer.cornerRadius = 6.0f;
  63. bgView.layer.masksToBounds = YES;
  64. UITextField* textField = [[UITextField alloc]initWithFrame:CGRectMake(0,0, KScreenWidth-28,49)];
  65. textField.tintColor = LZ7C69FEColor;
  66. textField.backgroundColor = LZF7F8FAColor;
  67. textField.layer.cornerRadius = 6.0f;
  68. textField.layer.masksToBounds = YES;
  69. textField.delegate = self;
  70. textField.text = self.youpaipnickName;
  71. textField.font = LCFont(14);
  72. textField.textColor = LZ273145Color;
  73. textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,12,49)];
  74. textField.leftViewMode = UITextFieldViewModeAlways;
  75. textField.clearButtonMode = UITextFieldViewModeAlways;
  76. [[textField valueForKey:@"_clearButton"] setImage:[UIImage imageNamed:@"vqu_images_D_nick_clean"] forState:UIControlStateNormal];
  77. NSString *holderText = @"输入昵称";
  78. NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
  79. [placeholder addAttribute:NSForegroundColorAttributeName value:LZD3D1D7Color range:NSMakeRange(0, holderText.length)];
  80. [placeholder addAttribute:NSFontAttributeName value:LCFont(14) range:NSMakeRange(0, holderText.length)];
  81. textField.attributedPlaceholder = placeholder;
  82. self.youpaiptextField = textField;
  83. [bgView addSubview:textField];
  84. // UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
  85. // clearButton.frame = CGRectMake(KScreenWidth-28-20-10, 29/2, 20, 20);
  86. // [clearButton addTarget:self action:@selector(clearButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
  87. // [clearButton setImage:[UIImage imageNamed:@"vqu_images_D_nick_clean"] forState:UIControlStateNormal];
  88. // [bgView addSubview:clearButton];
  89. // self.clearButton = clearButton;
  90. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youpaiftextDidChange) name:UITextFieldTextDidChangeNotification object:textField];
  91. }
  92. //- (void)clearButtonDidClick: (UIButton *)button {
  93. // self.textField.text = nil;
  94. // }
  95. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  96. [self.youpaiptextField resignFirstResponder];
  97. return YES;
  98. }
  99. - (void)youpaiftextDidChange{
  100. NSLog(@"%@",self.youpaiptextField.text);
  101. if ([self.youpaiptextField.text isEqualToString:self.youpaipnickName]) {
  102. self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifSaveRemark) title:@"保存" font:LCFont15 titleColor:LZA3AABEColor highlightedColor:LZA3AABEColor titleEdgeInsets:UIEdgeInsetsZero];
  103. self.navigationItem.rightBarButtonItem.enabled = NO;
  104. }else{
  105. self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifSaveRemark) title:@"保存" font:LCFont15 titleColor:LZ7C69FEColor highlightedColor:LZ7C69FEColor titleEdgeInsets:UIEdgeInsetsZero];
  106. self.navigationItem.rightBarButtonItem.enabled = YES;
  107. }
  108. if(self.youpaiptextField.text.length>10){
  109. self.youpaiptextField.text = [self.youpaiptextField.text substringToIndex:10];
  110. }
  111. }
  112. - (void)youpaifSaveRemark{
  113. //加一个正则判断昵称是否符合规则 正则限制/[\x{4e00}-\x{9fa5}\w]+$/u 长度限制2~12个utf8字符
  114. if (self.youpaiptextField.text.length<1 || self.youpaiptextField.text.length>10) {
  115. [ZCHUDHelper showTitle:@"昵称长度为1~10个字符"];
  116. }else{
  117. [LCHttpHelper requestWithURLString:UpdateUserMsg parameters:@{@"nickname":self.youpaiptextField.text}
  118. needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  119. NSDictionary* dict = (NSDictionary*)responseObject;
  120. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  121. if (code==0) {//成功
  122. YOUPAILCUserModel* userModel = [LCSaveModel getUserModel];
  123. userModel.youpaipuserinfo.youpaipnickname = self.youpaiptextField.text;
  124. [LCSaveModel saveUserModel:userModel];
  125. if (self.youpaipblock) {
  126. self.youpaipblock(self.youpaiptextField.text);
  127. }
  128. [self.navigationController popViewControllerAnimated:YES];
  129. }
  130. } failure:^(NSError *error) {
  131. }];
  132. }
  133. }
  134. - (void)dealloc
  135. {
  136. [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:self.youpaiptextField];
  137. }
  138. @end