// // editMobileController.m // ThePaperBase // // Created by Huixin on 15/8/25. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "editMobileController.h" @interface editMobileController () { NSString *phoneNum; NSString *verificationCode; } @property(nonatomic, strong)UIImageView *mobileImageView; @property(nonatomic, strong)UITextField *mobileField; @property(nonatomic, strong)UIView *mobileLine; @property(nonatomic, strong)UIImageView *verificationCodeImageView; @property(nonatomic, strong)UITextField *verificationCodeField; @property(nonatomic, strong)UIView *verificationCodeLine; @property(nonatomic, strong)UIButton *getCodeBtn; @property(nonatomic, strong)TPCustomButton *confirmBtn; @end @implementation editMobileController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; self.titleStr = @"修改手机"; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)]; [self.view addGestureRecognizer:tapGesture]; [self.view addSubview:self.mobileField]; [self.view addSubview:self.mobileImageView]; [self.view addSubview:self.mobileLine]; [self.view addSubview:self.getCodeBtn]; [self.view addSubview:self.verificationCodeImageView]; [self.view addSubview:self.verificationCodeField]; [self.view addSubview:self.verificationCodeLine]; [self.view addSubview:self.confirmBtn]; [self.mobileField becomeFirstResponder]; [self layoutViews]; } - (void)layoutViews { [self.mobileImageView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.left).offset(20); make.centerY.equalTo(self.mobileField.centerY); make.width.mas_equalTo(@15.5); make.height.mas_equalTo(@22); }]; [self.mobileField makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.naviBar.bottom).offset(38); make.left.equalTo(self.view.left).offset(50); make.right.equalTo(self.view.right).offset(-20); make.height.mas_equalTo(@19); }]; [self.mobileLine makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.mobileField.bottom).offset(7); make.left.equalTo(self.mobileImageView.left); make.right.equalTo(self.mobileField.right); make.height.mas_equalTo(@1); }]; [self.getCodeBtn makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.mobileLine.bottom).offset(30); make.centerX.equalTo(self.view.centerX); make.width.mas_equalTo(@80); make.height.mas_equalTo(@20); }]; [self.verificationCodeImageView makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.verificationCodeField.centerY); make.left.equalTo(self.view.left).offset(20); make.width.mas_equalTo(@20); make.height.mas_equalTo(@15); }]; [self.verificationCodeField makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.getCodeBtn.bottom).offset(38); make.left.equalTo(self.view.left).offset(50); make.right.equalTo(self.view.right).offset(-20); make.height.mas_equalTo(@19); }]; [self.verificationCodeLine makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.verificationCodeField.bottom).offset(7); make.left.equalTo(self.verificationCodeImageView.left); make.right.equalTo(self.verificationCodeField.right); make.height.mas_equalTo(@1); }]; [self.confirmBtn makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.verificationCodeLine.bottom).offset(35); make.left.equalTo(self.view.left).offset(20); make.right.equalTo(self.view.right).offset(-20); make.height.equalTo(self.confirmBtn.width).multipliedBy(0.15); }]; } #pragma mark - get method - (UIImageView*)mobileImageView { if (!_mobileImageView) { _mobileImageView = [[UIImageView alloc] init]; _mobileImageView.image = Image(@"login/mobile.png"); } return _mobileImageView; } - (UITextField*)mobileField { if (!_mobileField) { _mobileField = [[UITextField alloc] init]; _mobileField.backgroundColor = [UIColor clearColor]; NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:@"请输入新的手机号" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:TextLightGray]}]; _mobileField.attributedPlaceholder = atrString; _mobileField.textColor = [UIColor colorWithHexString:TextBlack]; _mobileField.font = appFont(TEXT_FOUR_LEVELSIZE, NO); _mobileField.textAlignment = NSTextAlignmentLeft; _mobileField.keyboardAppearance = UIKeyboardAppearanceDefault; if ([[TPUserDefault instance].isNightMode intValue] > 0) { _mobileField.keyboardAppearance = UIKeyboardAppearanceDark; } _mobileField.keyboardType = UIKeyboardTypeNumberPad; _mobileField.clearButtonMode = UITextFieldViewModeWhileEditing; _mobileField.delegate = self; [_mobileField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; } return _mobileField; } - (UIView*)mobileLine { if (!_mobileLine) { _mobileLine = [[UIView alloc] init]; _mobileLine.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; } return _mobileLine; } - (UIButton*)getCodeBtn { if (!_getCodeBtn) { _getCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal]; [_getCodeBtn setTitleColor:[UIColor colorWithHexString:BLUECOLOR] forState:UIControlStateNormal]; [_getCodeBtn setTitleColor:[UIColor colorWithHexString:BUTTONSELECTBACK] forState:UIControlStateHighlighted]; _getCodeBtn.titleLabel.font = appFont(TEXT_FIVE_LEVELSIZE, NO); _getCodeBtn.layer.borderColor = [UIColor colorWithHexString:BLUECOLOR].CGColor; _getCodeBtn.layer.cornerRadius = 4.f; _getCodeBtn.layer.borderWidth = 1; [_getCodeBtn addTarget:self action:@selector(getVerificationCode:) forControlEvents:UIControlEventTouchUpInside]; } return _getCodeBtn; } - (UIImageView*)verificationCodeImageView { if (!_verificationCodeImageView) { _verificationCodeImageView = [[UIImageView alloc] init]; _verificationCodeImageView.image = Image(@"login/pin.png"); } return _verificationCodeImageView; } - (UITextField*)verificationCodeField { if (!_verificationCodeField) { _verificationCodeField = [[UITextField alloc] init]; _verificationCodeField.backgroundColor = [UIColor clearColor]; NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:@"请输入手机中的验证码" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:TextLightGray]}]; _verificationCodeField.attributedPlaceholder = atrString; _verificationCodeField.textColor = [UIColor colorWithHexString:TextBlack]; _verificationCodeField.font = appFont(TEXT_FOUR_LEVELSIZE, NO); _verificationCodeField.textAlignment = NSTextAlignmentLeft; _verificationCodeField.keyboardAppearance = UIKeyboardAppearanceDefault; if ([[TPUserDefault instance].isNightMode intValue] > 0) { _verificationCodeField.keyboardAppearance = UIKeyboardAppearanceDark; } _verificationCodeField.keyboardType = UIKeyboardTypeNumberPad; _verificationCodeField.clearButtonMode = UITextFieldViewModeWhileEditing; _verificationCodeField.delegate = self; [_verificationCodeField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; } return _verificationCodeField; } - (UIView*)verificationCodeLine { if (!_verificationCodeLine) { _verificationCodeLine = [[UIView alloc] init]; _verificationCodeLine.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; } return _verificationCodeLine; } - (TPCustomButton*)confirmBtn { if (!_confirmBtn) { _confirmBtn = [[TPCustomButton alloc] init]; _confirmBtn.title = @"确认"; [_confirmBtn addTarget:self action:@selector(confirmEvent) forControlEvents:UIControlEventTouchUpInside]; } return _confirmBtn; } #pragma mark - textfield delegate - (void)textFieldDidChange:(UITextField *)textField { if ([textField.text isMatchedByRegex:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"]) { textField.text = disable_emoji(textField.text); } if (textField == self.mobileField) { phoneNum = textField.text; }else if (textField == self.verificationCodeField) { verificationCode = textField.text; } } - (BOOL)textFieldShouldReturn:(UITextField *)textField { if (textField == _mobileField) { [_verificationCodeField becomeFirstResponder]; } else if (textField == _verificationCodeField) { [self confirmEvent]; } return YES; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if ([textField isFirstResponder]) { if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) { return NO; } } return YES; } #pragma mark - btn event handler - (void)getVerificationCode:(UIButton*)btn { [self closeKeyBoard]; if (isBlankString(phoneNum)) { ShowTextMessage(@"请输入手机号码"); return; } NSDictionary *dic = @{@"verType":@"4",@"mail":phoneNum}; [Remote doJsonActionWithBlock:0 requestUrl:getVerCodeURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { if ([responseData[@"resultCode"] intValue] == 1) { ShowTextMessage(responseData[@"resultMsg"]); } }else { ShowTextMessage(message); } }]; } - (void)confirmEvent { [self closeKeyBoard]; if (isBlankString(phoneNum)) { ShowTextMessage(@"请输入手机号码"); return; } if (isBlankString(verificationCode)) { ShowTextMessage(@"请输入验证码"); return; } NSDictionary *dic = @{@"verType":@"4",@"mail":phoneNum,@"verCode":verificationCode}; [Remote doJsonAction:0 requestUrl:checkVerCodeURL parameter:dic delegate:self]; } #pragma mark - remote delegate - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responseData { if (actionTag == 0) { if ([responseData[@"resultCode"] intValue] == 1) { userBO *user = [TPUserDefault instance].userBO; if ([user.name isEqualToString:user.mobile]) user.name = phoneNum; user.mobile = phoneNum; [TPUserDefault instance].userBO = user; [self.navigationController popViewControllerAnimated:YES]; }else { ShowTextMessage(responseData[@"resultMsg"]); } } } - (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code{ ShowTextMessage(message); } #pragma mark - tap handler - (void)tap:(UITapGestureRecognizer*)sender { [self closeKeyBoard]; } #pragma mark - keyBoard event - (void)closeKeyBoard { [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]; } - (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