// // MMMessageVerificationVC.m // MingMen // // Created by 肖雨 on 2017/3/11. // Copyright © 2017年 肖雨. All rights reserved. // #import "NewModifyPhoneViewController.h" #import "NewRegistCell.h" @interface NewModifyPhoneViewController () { NSMutableArray *dataArray; UITableView *mainTableView; UITextField *phoneTextField; UITextField *verificationCodeTextField; UIButton *verificationCodeButton; } @end @implementation NewModifyPhoneViewController -(void)dealloc { NSLog(@"修改手机号界面销毁"); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setNavTitle:@"修改手机号"]; [self dataInitialization]; [self loadsView]; } #pragma mark - 数据初始化 -(void)dataInitialization { phoneTextField = nil; verificationCodeTextField = nil; verificationCodeButton = nil; dataArray = NewMutableArrayInit; for (int i=0; i<2; i++) { NSDictionary *dataDic = [NSDictionary dictionary]; switch (i) { case 0: dataDic = @{@"nameKey":@"手机号",@"imageKey":@"手机号码",@"placeholderKey":@"请输入新的手机号"}; [dataArray addObject:dataDic]; break; case 1: dataDic = @{@"nameKey":@"验证码",@"imageKey":@"短信",@"placeholderKey":@"请输入短信验证码"}; [dataArray addObject:dataDic]; break; default: break; } } } #pragma mark - UI初始化 -(void)loadsView { [self.view addSubview:mainTableView = [NewControlPackage tableViewInitWithFrame:CGRectMake(0, ViewStartY, SCREEN_WIDTH, SCREEN_HEIGHT-ViewStartY) backgroundColor:NewWhiteColor style:0 delegate:self dataSource:self showsHorizontalScrollIndicator:NO showsVerticalScrollIndicator:NO hidden:NO tag:100 userInteractionEnabled:YES]]; UIView *view; mainTableView.tableFooterView = view = [NewControlPackage viewInitWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 84) backgroundColor:NewClearColor hidden:NO tag:101 userInteractionEnabled:YES]; UIImageView *lineImge; [view addSubview:lineImge = [NewControlPackage imageViewInitWithFrame:CGRectMake(15, 0, SCREEN_WIDTH-15, .6) image:nil highlightedImage:nil backgroundColor:NewCellLineColor tag:102 hidden:NO userInteractionEnabled:YES]]; UIButton *confirm; [view addSubview:confirm = [NewControlPackage buttonInitWithTitle:@"确定" Frame:CGRectMake(20, 40, SCREEN_WIDTH-40, 36) backgroundImage:nil backgroundImageHighlighted:nil backgroundColor:NewNavigationColor textColor:NewWhiteColor textAlignment:UIControlContentHorizontalAlignmentCenter font:NewFont(14) tag:105 target:self action:@selector(touchConfirm) hidden:NO userInteractionEnabled:YES]]; ViewRadius(confirm, 8); } #pragma mark - UITableView Delegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return dataArray.count; } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *Cell = @"Cell"; NewRegistCell * cell = [tableView dequeueReusableCellWithIdentifier:Cell]; if (cell == nil) { cell = [[NewRegistCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cell]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.accessoryType = UITableViewCellAccessoryNone; cell.image.image = NewImageNamed(dataArray[indexPath.row][@"imageKey"]); cell.title.text = dataArray[indexPath.row][@"nameKey"]; if (verificationCodeTextField == nil) { //获取验证码frame float vcwidth = 66; float vcheight = cell.height-20; float vcx = SCREEN_WIDTH-vcwidth-20; float vcy = 10; //输入框frame float tx = 110; float ty = 2; float twidth = SCREEN_WIDTH-tx-20; float theight = cell.height-2; UITextField *textField; [cell.contentView addSubview:textField = [NewControlPackage textFieldInitWithFrame:CGRectMake(tx, ty, twidth, theight) backgroundImage:nil backgroundColor:NewClearColor textColor:NewGrayColor placeholder:dataArray[indexPath.row][@"placeholderKey"] hidden:NO tag:100+(int)indexPath.row font:NewFont(14) textAlignment:NSTextAlignmentLeft clearButtonMode:NO clearsOnBeginEditing:NO adjustsFontSizeToFitWidth:NO secureTextEntry:NO keyboardType:UIKeyboardTypeDefault returnKeyType:UIReturnKeyDefault userInteractionEnabled:YES]]; [textField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged]; if (indexPath.row==0) { phoneTextField = textField; //phoneTextField.keyboardType = UIKeyboardTypeNumberPad; }else if (indexPath.row==1) { verificationCodeTextField = textField; [cell.contentView addSubview:verificationCodeButton = [NewControlPackage buttonInitWithTitle:@"获取验证码" Frame:CGRectMake(vcx, vcy, vcwidth, vcheight) backgroundImage:nil backgroundImageHighlighted:nil backgroundColor:NewLightGrayColor textColor:NewWhiteColor textAlignment:UIControlContentHorizontalAlignmentCenter font:NewFont(10) tag:99 target:self action:@selector(touchVerificationCode:) hidden:NO userInteractionEnabled:YES]]; ViewRadius(verificationCodeButton, 10); } } return cell; } #pragma mark - 作字符输入限制 - (void)textFieldEditingChanged:(UITextField *)textField { if (textField == phoneTextField) { if ([textField.text length]>11) { textField.text=[textField.text substringToIndex:11];//手机号码11位 } }else if (textField == verificationCodeTextField) { if ([textField.text length]>4) { textField.text=[textField.text substringToIndex:4];//验证码4位 } } } #pragma mark - 获取验证码 - (void)touchVerificationCode:(UIButton *)sender { if (![self verification:nil]) { return; } sender.frame = CGRectMake(sender.left, sender.top, 80, sender.height); [self verificationCode:60 sender:sender]; } - (void)verificationCode:(NSInteger)code sender:(UIButton *)sender{ NSString *str=[NSString stringWithFormat:@"%d秒后重新发送",(int)code]; [sender setTitle:str forState:UIControlStateNormal]; sender.enabled=NO; if (code==0) { sender.enabled=YES; [sender setTitle:@"获取验证码" forState:UIControlStateNormal]; sender.frame = CGRectMake(sender.left, sender.top, 66, sender.height); return; } code--; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self verificationCode:code sender:sender]; }); } #pragma mark - 确定 - (void)touchConfirm { if ([self verification:@"已下发短信验证码"]) { } }; - (BOOL)verification:(NSString *)status { if (phoneTextField.text.length <= 0) { [self.view makeToast:@"手机号码输入有误" duration:1.5 position:CSToastPositionCenter]; return NO; } if ([status isEqualToString:@"已下发短信验证码"]) { if (verificationCodeTextField.text.length <= 0) { [self.view makeToast:@"验证码输入有误" duration:1.5 position:CSToastPositionCenter]; return NO; } } return YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #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