ifish/Ifish/controllers/FishTinkController/maincontroller/CenterontrolControllers/RuSunTemp/RuSunIndependence/RunSunChangeNameViewControl...

295 lines
10 KiB
Objective-C

//
// RunSunChangeNameViewController.m
// Ifish
//
// Created by imac on 2017/6/2.
// Copyright © 2017年 lianlian. All rights reserved.
//
#import "RunSunChangeNameViewController.h"
#import "RuSunChangeNameTitleViewCell.h"
#import "RuSunChangeNameTextViewCell.h"
#define RUSUN_DEVICENAME 100001
#define RUSUNCHCELL_CH1 20000
#define RUSUNCHCELL_CH2 20001
#define RUSUNCHCELL_CH3 20002
#define RUSUNCHCELL_CH4 20003
#import "RunSunCHNameCustom.h"
#import "RunSunCHControl.h"
#import "UserExtendataArchaver.h"
@interface RunSunChangeNameViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic,strong) UITableView *tableView;
@end
@implementation RunSunChangeNameViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self addTitleViewWithTitle:@"修改名称"];
UIButton *tureButton = [UIButton buttonWithType:UIButtonTypeCustom];
tureButton.frame = CGRectMake(kScreenSize.width - 100, 10, 60, 60);
[tureButton setTitle:@"确定" forState:UIControlStateNormal];
[tureButton addTarget: self action: @selector(sureAction) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem *right=[[UIBarButtonItem alloc]initWithCustomView:tureButton];
self.navigationItem.rightBarButtonItem=right;
self.name = self.device.showName;
[self creatTab];
}
-(void)sureAction{
//RunSunCHNameCustom 本地处理 保存按钮名称 不与服务器交互
RunSunCHControl *ruSunch = [[RunSunCHControl alloc] init];
RuSunChangeNameTextViewCell*cell1 = [self.view viewWithTag:RUSUNCHCELL_CH1];
if (cell1.chName.text.length>=3) {
[self.view makeToast:@"CH1不能超过3个字"];
return;
}
if (cell1.chName.text.length==0) {
[self.view makeToast:@"CH1不能为空"];
return;
}
ruSunch.ch1name = cell1.chName.text;
RuSunChangeNameTextViewCell*cell2 = [self.view viewWithTag:RUSUNCHCELL_CH2];
if (cell2.chName.text.length>=3) {
[self.view makeToast:@"CH2不能超过3个字"];
return;
}
if (cell2.chName.text.length==0) {
[self.view makeToast:@"CH2不能为空"];
return;
}
ruSunch.ch2name = cell2.chName.text;
RuSunChangeNameTextViewCell*cell3 = [self.view viewWithTag:RUSUNCHCELL_CH3];
if (cell3.chName.text.length>=3) {
[self.view makeToast:@"CH3不能超过3个字"];
return;
}
if (cell3.chName.text.length==0) {
[self.view makeToast:@"CH3不能为空"];
return;
}
ruSunch.ch3name = cell3.chName.text;
RuSunChangeNameTextViewCell*cell4 = [self.view viewWithTag:RUSUNCHCELL_CH4];
if (cell4.chName.text.length>=3) {
[self.view makeToast:@"CH4不能超过3个字"];
return;
}
if (cell4.chName.text.length==0) {
[self.view makeToast:@"CH4不能为空"];
return;
}
ruSunch.ch4name = cell4.chName.text;
ruSunch.macAd = self.device.macAddress;
[RunSunCHNameCustom saveCHname:ruSunch];
RuSunChangeNameTitleViewCell*cell = [self.view viewWithTag:RUSUN_DEVICENAME];
[cell.deviceName 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: cell.deviceName.text forKey:@"showName"];
if (cell.deviceName.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:@"%@",cell.deviceName.text];
NSMutableArray *newDeviceArr =[[NSMutableArray alloc] init];
if (self.NameWithBlock) {
self.NameWithBlock(strName);
}
[cell.deviceName 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)creatTab{
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStyleGrouped];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.scrollEnabled = YES;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.showsVerticalScrollIndicator = NO;
self.tableView.backgroundColor = RGB(242, 242, 242);
[self.view addSubview:self.tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section==0) {
return 1;
}else{
return 6;
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==0) {
RuSunChangeNameTitleViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"RuSunChangeNameTitleViewCell"];
if (cell==nil) {
cell= [[[NSBundle mainBundle]loadNibNamed:@"RuSunChangeNameTitleViewCell" owner:self options:nil]lastObject];
}
cell.tag = RUSUN_DEVICENAME;
cell.deviceName.text = self.name;
return cell;
}
RuSunChangeNameTextViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"RuSunChangeNameTextViewCell"];
if (cell==nil) {
cell= [[[NSBundle mainBundle]loadNibNamed:@"RuSunChangeNameTextViewCell" owner:self options:nil]lastObject];
}
//[cell freshImgAt:indexPath];
//RunSunCHControl * ruSunch = [RunSunCHNameCustom currentdevice:self.device.macAddress];
//[cell loadChnameAt:indexPath with:ruSunch];
cell.chName.delegate = self;
cell.tag = 20000 + indexPath.row;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 10;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (section==1) {
UIView *holdView = [[UIView alloc] init];
holdView.frame =CGRectMake(0, 0, kScreenSize.width, 10);
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redact_line"]];
img.frame = CGRectMake(19, 0, 12, 10);
[holdView addSubview:img];
return holdView;
}
return nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)popLastView{
[self.navigationController popViewControllerAnimated:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end