| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- //
- // NTESAliasSettingViewController.m
- // NIM
- //
- // Created by chris on 15/11/5.
- // Copyright © 2015年 Netease. All rights reserved.
- //
- #import "NTESAliasSettingViewController.h"
- #import "NIMCommonTableDelegate.h"
- #import "NIMCommonTableData.h"
- #import "SVProgressHUD.h"
- #import "UIView+Toast.h"
- #import "HttpRequest.h"
- #import "User.h"
- #import "NIMKit.h"
- @interface NTESAliasSettingViewController()
- @property (nonatomic,strong) NIMCommonTableDelegate *delegator;
- @property (nonatomic,copy ) NSArray *data;
- @property (nonatomic,copy ) NSString *alias;
- @property (nonatomic,assign) NSInteger inputLimit;
- @property (nonatomic,strong) NIMUser *user;
- @end
- @implementation NTESAliasSettingViewController
- - (instancetype)initWithUserId:(NSString *)userId{
- self = [super initWithNibName:nil bundle:nil];
- if (self) {
- _inputLimit = 16;
- _user = [[NIMSDK sharedSDK].userManager userInfo:userId];
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self setUpNav];
- self.navigationItem.title = @"备注名";
- __weak typeof(self) wself = self;
- self.alias = [[NIMKit sharedKit] getAlias:self.user.userId] ? [[NIMKit sharedKit] getAlias:self.user.userId] : @"";
- [self buildData];
- self.delegator = [[NIMCommonTableDelegate alloc] initWithTableData:^NSArray *{
- return wself.data;
- }];
-
- self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
- self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
- [self.view addSubview:self.tableView];
- self.tableView.backgroundColor = UIColorFromRGB(0xe3e6ea);
- self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- self.tableView.delegate = self.delegator;
- self.tableView.dataSource = self.delegator;
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTextFieldChanged:) name:UITextFieldTextDidChangeNotification object:nil];
- }
- - (void)viewDidAppear:(BOOL)animated{
- [super viewDidAppear:animated];
- for (UITableViewCell *cell in self.tableView.visibleCells) {
- for (UIView *subView in cell.subviews) {
- if ([subView isKindOfClass:[UITextField class]]) {
- [subView becomeFirstResponder];
- break;
- }
- }
- }
- }
- - (void)dealloc{
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)setUpNav{
- self.navigationItem.title = @"签名";
- self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(onDone:)];
- self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
- }
- - (void)onDone:(id)sender{
- [self.view endEditing:YES];
- if (self.alias.length > self.inputLimit) {
- [self.view makeToast:@"备注名过长" duration:2.0 position:CSToastPositionCenter];
- return;
- }
- [SVProgressHUD show];
- __weak typeof(self) wself = self;
-
- [[HttpRequest shared] updateAlias:self.user.userId alias:self.alias success:^{
- [SVProgressHUD dismiss];
- [[NIMKit sharedKit] setAlias:wself.user.userId alias:wself.alias];
- [wself.navigationController.view makeToast:@"备注名设置成功"
- duration:2
- position:CSToastPositionCenter];
- [wself.navigationController popViewControllerAnimated:YES];
- } failure:^{
- [SVProgressHUD dismiss];
- [wself.view makeToast:@"备注名设置失败,请重试"
- duration:2
- position:CSToastPositionCenter];
- }];
- }
- - (void)buildData{
- NSArray *data = @[
- @{
- HeaderTitle:@"",
- RowContent :@[
- @{
- ExtraInfo : self.alias.length ? self.alias : @"",
- CellClass : @"NTESTextSettingCell",
- RowHeight : @(50),
- },
- ],
- },
- ];
- self.data = [NIMCommonTableSection sectionsWithData:data];
- }
- #pragma mark - UITextFieldDelegate
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
- if ([string length] == 0 && range.length > 0)
- {
- return YES;
- }
- NSString *genString = [textField.text stringByReplacingCharactersInRange:range withString:string];
- if (self.inputLimit && genString.length > self.inputLimit) {
- return NO;
- }
- return YES;
- }
- - (void)onTextFieldChanged:(NSNotification *)notification{
- UITextField *textField = notification.object;
- self.alias = textField.text;
- }
- #pragma mark - 旋转处理 (iOS7)
- - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
- {
- [self.tableView reloadData];
- }
- @end
|