NTESAliasSettingViewController.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // NTESAliasSettingViewController.m
  3. // NIM
  4. //
  5. // Created by chris on 15/11/5.
  6. // Copyright © 2015年 Netease. All rights reserved.
  7. //
  8. #import "NTESAliasSettingViewController.h"
  9. #import "NIMCommonTableDelegate.h"
  10. #import "NIMCommonTableData.h"
  11. #import "SVProgressHUD.h"
  12. #import "UIView+Toast.h"
  13. #import "HttpRequest.h"
  14. #import "User.h"
  15. #import "NIMKit.h"
  16. @interface NTESAliasSettingViewController()
  17. @property (nonatomic,strong) NIMCommonTableDelegate *delegator;
  18. @property (nonatomic,copy ) NSArray *data;
  19. @property (nonatomic,copy ) NSString *alias;
  20. @property (nonatomic,assign) NSInteger inputLimit;
  21. @property (nonatomic,strong) NIMUser *user;
  22. @end
  23. @implementation NTESAliasSettingViewController
  24. - (instancetype)initWithUserId:(NSString *)userId{
  25. self = [super initWithNibName:nil bundle:nil];
  26. if (self) {
  27. _inputLimit = 16;
  28. _user = [[NIMSDK sharedSDK].userManager userInfo:userId];
  29. }
  30. return self;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. [self setUpNav];
  35. self.navigationItem.title = @"备注名";
  36. __weak typeof(self) wself = self;
  37. self.alias = [[NIMKit sharedKit] getAlias:self.user.userId] ? [[NIMKit sharedKit] getAlias:self.user.userId] : @"";
  38. [self buildData];
  39. self.delegator = [[NIMCommonTableDelegate alloc] initWithTableData:^NSArray *{
  40. return wself.data;
  41. }];
  42. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
  43. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  44. [self.view addSubview:self.tableView];
  45. self.tableView.backgroundColor = UIColorFromRGB(0xe3e6ea);
  46. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  47. self.tableView.delegate = self.delegator;
  48. self.tableView.dataSource = self.delegator;
  49. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTextFieldChanged:) name:UITextFieldTextDidChangeNotification object:nil];
  50. }
  51. - (void)viewDidAppear:(BOOL)animated{
  52. [super viewDidAppear:animated];
  53. for (UITableViewCell *cell in self.tableView.visibleCells) {
  54. for (UIView *subView in cell.subviews) {
  55. if ([subView isKindOfClass:[UITextField class]]) {
  56. [subView becomeFirstResponder];
  57. break;
  58. }
  59. }
  60. }
  61. }
  62. - (void)dealloc{
  63. [[NSNotificationCenter defaultCenter] removeObserver:self];
  64. }
  65. - (void)setUpNav{
  66. self.navigationItem.title = @"签名";
  67. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(onDone:)];
  68. self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
  69. }
  70. - (void)onDone:(id)sender{
  71. [self.view endEditing:YES];
  72. if (self.alias.length > self.inputLimit) {
  73. [self.view makeToast:@"备注名过长" duration:2.0 position:CSToastPositionCenter];
  74. return;
  75. }
  76. [SVProgressHUD show];
  77. __weak typeof(self) wself = self;
  78. [[HttpRequest shared] updateAlias:self.user.userId alias:self.alias success:^{
  79. [SVProgressHUD dismiss];
  80. [[NIMKit sharedKit] setAlias:wself.user.userId alias:wself.alias];
  81. [wself.navigationController.view makeToast:@"备注名设置成功"
  82. duration:2
  83. position:CSToastPositionCenter];
  84. [wself.navigationController popViewControllerAnimated:YES];
  85. } failure:^{
  86. [SVProgressHUD dismiss];
  87. [wself.view makeToast:@"备注名设置失败,请重试"
  88. duration:2
  89. position:CSToastPositionCenter];
  90. }];
  91. }
  92. - (void)buildData{
  93. NSArray *data = @[
  94. @{
  95. HeaderTitle:@"",
  96. RowContent :@[
  97. @{
  98. ExtraInfo : self.alias.length ? self.alias : @"",
  99. CellClass : @"NTESTextSettingCell",
  100. RowHeight : @(50),
  101. },
  102. ],
  103. },
  104. ];
  105. self.data = [NIMCommonTableSection sectionsWithData:data];
  106. }
  107. #pragma mark - UITextFieldDelegate
  108. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  109. if ([string length] == 0 && range.length > 0)
  110. {
  111. return YES;
  112. }
  113. NSString *genString = [textField.text stringByReplacingCharactersInRange:range withString:string];
  114. if (self.inputLimit && genString.length > self.inputLimit) {
  115. return NO;
  116. }
  117. return YES;
  118. }
  119. - (void)onTextFieldChanged:(NSNotification *)notification{
  120. UITextField *textField = notification.object;
  121. self.alias = textField.text;
  122. }
  123. #pragma mark - 旋转处理 (iOS7)
  124. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  125. {
  126. [self.tableView reloadData];
  127. }
  128. @end