// // SetNameTextViewController.m // Ifish // // Created by ifish7-ios on 16/3/30. // Copyright © 2016年 imac. All rights reserved. // #import "SetNameTextViewController.h" #import "AFNetworking.h" #import "MyMD5.h" #import "Toast+UIView.h" @interface SetNameTextViewController () @property (nonatomic,strong) UIButton *tureButton; @property (nonatomic,strong) UIBarButtonItem *backItem; @property (nonatomic,strong) UITextField *field; @property (nonatomic,copy) NSString *nameWithText; @end @implementation SetNameTextViewController - (void)viewDidLoad { [super viewDidLoad]; [self creatNavRightButton]; [self creatTextField]; self.view.backgroundColor = COLOR_MIAN; if (self.deviceType ==0) { self.title = @"修改水族箱名称"; }else if (self.deviceType ==1){ self.title = @"修改摄像头名称"; } self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName :[UIColor whiteColor]}; UILabel *label = [[UILabel alloc] init]; label.textAlignment = NSTextAlignmentLeft; // Do any additional setup after loading the view. } -(void)creatNavRightButton{ _tureButton = [UIButton buttonWithType:UIButtonTypeCustom]; _tureButton.frame = CGRectMake(kScreenSize.width - 100, 10, 60, 60); [_tureButton setTitle:@"确定" forState:UIControlStateNormal]; [_tureButton setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; if (self.deviceType ==0) { [_tureButton addTarget: self action: @selector(goBackActionClick) forControlEvents: UIControlEventTouchUpInside]; }else if (self.deviceType ==1){ [_tureButton addTarget: self action: @selector(cameraSureAction) forControlEvents: UIControlEventTouchUpInside]; } _backItem=[[UIBarButtonItem alloc]initWithCustomView:_tureButton]; self.navigationItem.rightBarButtonItem=_backItem; } -(void)creatTextField{ self.field = [[UITextField alloc] initWithFrame:CGRectMake(20, 84, kScreenSize.width - 40, 60)]; self.field.backgroundColor = [UIColor whiteColor]; self.field.layer.cornerRadius = 10; self.field.text = self.name; self.field.textColor = [UIColor blackColor]; self.field.placeholder = @"请修改名字"; //NSLog(@"--执行了TextField--%@",self.field.text); self.field.clearButtonMode = UITextFieldViewModeWhileEditing; //[self.field becomeFirstResponder]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 84 + 60 + 20, 200, 20)]; label.text = @"给设备取个名字吧!" ; label.textColor = [UIColor whiteColor]; label.textAlignment = NSTextAlignmentLeft; label.font = [UIFont systemFontOfSize:14]; [self.view addSubview:label]; [self.view addSubview:self.field]; } -(void)goBackActionClick{ [_field resignFirstResponder]; if (self.name!=0) { AFHTTPRequestOperationManager*mannager=[AFHTTPRequestOperationManager manager]; mannager.responseSerializer=[AFHTTPResponseSerializer serializer]; NSMutableDictionary * para = [NSMutableDictionary dictionary]; NSArray*deviceArry=[[DataCenter defaultDtacenter]valueForKey:@"deviceInfo"]; NSString *priId=self.device.deviceId; ; NSString*userId=self.device.userId; [para setValue:priId forKey:@"priId.deviceId"]; [para setValue:userId forKey:@"priId.userId"]; [para setValue: _field.text forKey:@"showName"]; if (_field.text.length <= 6) { [mannager POST:kUpdateDeviceUser parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) { if (responseObject) { NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]; NSLog(@"result:%@",resultDic[@"result"]); if ([resultDic[@"result"] isEqualToString:@"100"]) { [self.view makeToast:@"修改成功"]; NSString *strName =[NSString stringWithFormat:@"%@",self.field.text]; NSMutableArray *newDeviceArr =[[NSMutableArray alloc] init]; if (self.NameWithBlock) { self.NameWithBlock(strName); } [_field resignFirstResponder]; //修改数据库中对应设备名称 for (DeviceModel *model in deviceArry) { if ([model.deviceId isEqual:self.device.deviceId]) { model.showName = strName; } [newDeviceArr addObject:model]; } [[DataCenter defaultDtacenter] setValue:newDeviceArr forKey:@"deviceInfo"]; [self performSelector:@selector(popLastView) withObject:self afterDelay:1.6]; }else if ([resultDic[@"result"] isEqualToString:@"101"]){ [self showTitle:@"" messsage:@"获取失败"]; }else if ([resultDic[@"result"] isEqualToString:@"301"]){ [self showTitle:@"" messsage:@"请求验证失败,请重新登陆"]; }else if ([resultDic[@"result"] isEqualToString:@"302"]){ [self showTitle:@"" messsage:@"请求被舍弃,未执行"]; }else if ([resultDic[@"result"] isEqualToString:@"207"]){ [self showTitle:@"" messsage:@"设备已解绑请推出后台重新登录"]; } } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { [self showTitle:@"" messsage:@"请求异常"]; }]; }else{ [self showTitle:@"" messsage:@"名称不能大于6个字"]; } }else{ [self showTitle:@"" messsage:@"设备名称不能为空"]; } } -(void)cameraSureAction{ //注意判断方法以及提示窗展示不同。 [_field resignFirstResponder]; if (_field.text.length == 0) { [self.view makeToast:@"设备名称不能为空"]; return; } if (_field.text.length > 6) { [self.view makeToast:@"名称不能大于6个字"]; return; } AFHTTPRequestOperationManager*mannager=[AFHTTPRequestOperationManager manager]; mannager.responseSerializer=[AFHTTPResponseSerializer serializer]; NSMutableDictionary * para = [NSMutableDictionary dictionary]; NSArray*camerArr=[[DataCenter defaultDtacenter]valueForKey:@"cameraArr"]; //IfishCameraModel*camdel=[camerArr objectAtIndex:self.changeDeviceNameIndex]; UserModel*userModel=[[DataCenter defaultDtacenter]valueForKey:@"UserLogIn"]; [para setValue:self.cameraModel.cameraId forKey:@"cameraId"]; [para setValue:userModel.userId forKey:@"userId"]; [para setValue: _field.text forKey:@"showName"]; [mannager POST:IfishUpdateCameraUser parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) { if (responseObject) { NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]; NSLog(@"result:%@",resultDic[@"result"]); if ([resultDic[@"result"] isEqualToString:@"100"]) { [self.view makeToast:@"修改成功"]; NSString *strName =[NSString stringWithFormat:@"%@",self.field.text]; NSMutableArray *newcameraArr =[[NSMutableArray alloc] init]; if (self.NameWithBlock) { self.NameWithBlock(strName); } //归档的弊端 改值麻烦 for (IfishCameraModel*camdel in camerArr) { if ([camdel.cameraId isEqual:self.cameraModel.cameraId]) { camdel.showName = strName; } [newcameraArr addObject:camdel]; } [[DataCenter defaultDtacenter] setValue:newcameraArr forKey:@"cameraArr"]; [self performSelector:@selector(popLastView) withObject:self afterDelay:1.6]; }else if ([resultDic[@"result"] isEqualToString:@"101"]){ [self.view makeToast:@"获取失败"]; }else if ([resultDic[@"result"] isEqualToString:@"301"]){ [self.view makeToast:@"请求验证失败,请重新登陆"]; }else if ([resultDic[@"result"] isEqualToString:@"302"]){ [self.view makeToast:@"请求被舍弃,未执行"]; }else if ([resultDic[@"result"] isEqualToString:@"207"]){ [self.view makeToast:@"设备已解绑请推出后台重新登录"]; } } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { [self.view makeToast:@"请求异常"]; }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)popLastView{ [self.navigationController popViewControllerAnimated:YES]; } @end