Я хочу, чтобы кнопка «Скрыть и показать» не конфликтовала с проверочным штифтом. ⇐ IOS
-
Anonymous
Я хочу, чтобы кнопка «Скрыть и показать» не конфликтовала с проверочным штифтом.
whenever user entering their pin and then press enter on the num pad no matter if they typed a wrong pin or the textfield is empty, there was no error message show up at the bottom.
However, whenever they pressed the hide/show button, the error work
I want it to show up not depending on the hide/show button
this is my code:
import Foundation import UIKit @objc func editingChanged(_ textField: UITextField) { switch textField { case tfconfirmPin: collectionConfirmPin.reloadData() default: break } } func textFieldShouldReturn(_ textField: UITextField) -> Bool { if textField == tfconfirmPin { tfconfirmPin.resignFirstResponder() onPinEntered() return false } return true } @objc private func onPinEntered() { let pin = tfconfirmPin.text ?? "" // Check if pin is empty before attempting verification guard !pin.isEmpty else { toastBottom("Vui lòng nhập mã pin để xác nhận giao dịch") return } doVerifyPIN(pin: pin) { [weak self] success, errorMessage in guard let self = self else { return } DispatchQueue.main.async { if success { self.pinVerificationDelegate?.didVerifyPINSuccess(pin: pin) self.dismiss(animated: true) } else { // Display "Mã PIN không chính xác" message regardless of the hide/show state self.toastBottom("Mã PIN không chính xác") } } } } @objc private func handleCollectionTap() { tfconfirmPin.becomeFirstResponder() } } extension ConfirmPinDialog { private func addView() { view.addSubview(contentView) contentView.setAnchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0) let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapOutside)) contentView.addGestureRecognizer(tapGestureRecognizer) viewConfirmPin.addSubview(lbTitle) lbTitle.setAnchor(top: viewConfirmPin.topAnchor, left: viewConfirmPin.leftAnchor, bottom: nil, right: viewConfirmPin.rightAnchor, paddingTop: (viewConfirmPin.frame.height - 80) / 2, paddingLeft: 0, paddingBottom: 0, paddingRight: 0) lbTitle.textAlignment = .center lbTitle.textColor = .XPrimaryColor lbTitle.font = .customBoldFont(size: 18) lbTitle.layer.masksToBounds = true lbTitle.backgroundColor = UIColor.white lbTitle.layer.cornerRadius = 7 lbTitle.heightAnchor.constraint(equalToConstant: 60).isActive = true lbTitle.tintColor = .clear lbTitle.frame = viewConfirmPin.bounds contentView.addSubview(viewConfirmPin) viewConfirmPin.setAnchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: view.frame.height * 0.3, paddingLeft: 70, paddingBottom: 30, paddingRight: 70) // Adjusted the left and right padding viewConfirmPin.heightAnchor.constraint(equalToConstant: 60).isActive = true // Adjusted the height viewConfirmPin.backgroundColor = UIColor.white viewConfirmPin.layer.cornerRadius = 7 // Adjusted the corner radius viewConfirmPin.tintColor = .clear // Add collectionConfirmPin viewConfirmPin.addSubview(collectionConfirmPin) collectionConfirmPin.setAnchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 15, paddingLeft: 15, paddingBottom: 0, paddingRight: 0) collectionConfirmPin.dataSource = self collectionConfirmPin.delegate = self collectionConfirmPin.viewHeight(20) collectionConfirmPin.viewWidth(100) collectionConfirmPin.register(EnterPasswordCell.self, forCellWithReuseIdentifier: "EnterPasswordCell") collectionConfirmPin.centerY(toView: viewConfirmPin) collectionConfirmPin.centerX(toView: viewConfirmPin) tfconfirmPin.isUserInteractionEnabled = true collectionConfirmPin.isUserInteractionEnabled = true let collectionTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleCollectionTap)) collectionConfirmPin.addGestureRecognizer(collectionTapGesture) viewConfirmPin.addSubview(tfconfirmPin) tfconfirmPin.setAnchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, height: 40) // Adjusted the height tfconfirmPin.centerX(toView: viewConfirmPin) tfconfirmPin.centerY(toView: viewConfirmPin) tfconfirmPin.tintColor = UIColor.clear tfconfirmPin.delegate = self tfconfirmPin.isSecureTextEntry = !isShowConfirmPin tfconfirmPin.addTarget(self, action: #selector(editingChanged), for: .editingChanged) tfconfirmPin.layer.borderColor = UIColor.lightGray.cgColor tfconfirmPin.layer.borderWidth = 0.1 tfconfirmPin.layer.cornerRadius = 10 tfconfirmPin.layer.masksToBounds = true viewConfirmPin.addSubview(imgconfirmPinEntry) imgconfirmPinEntry.setAnchor(top: nil, left: tfconfirmPin.rightAnchor, bottom: nil, right: viewConfirmPin.rightAnchor, paddingTop: 0, paddingLeft: 15, paddingBottom: 0, paddingRight: 15, width: 40, height: 40) imgconfirmPinEntry.centerY(toView: viewConfirmPin) imgconfirmPinEntry.isUserInteractionEnabled = true imgconfirmPinEntry.addTarget(self, action: #selector(passwordEntry), for: .touchUpInside) imgconfirmPinEntry.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 5) viewConfirmPin.bringSubviewToFront(imgconfirmPinEntry) } @objc private func handleTapOutside() { dismissDialog() } } extension ConfirmPinDialog { @objc func passwordEntry() { isShowConfirmPin.toggle() tfconfirmPin.isSecureTextEntry = !isShowConfirmPin if isShowConfirmPin { imgconfirmPinEntry.setImage(UIImage(named: "ic_show_password"), for: .normal) } else { imgconfirmPinEntry.setImage(UIImage(named: "ic_hide_password"), for: .normal) } collectionConfirmPin.reloadData() } } extension ConfirmPinDialog: UICollectionViewDelegate,UICollectionViewDataSource,UITextFieldDelegate { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 6 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "EnterPasswordCell", for: indexPath) as! EnterPasswordCell var enteredPin = "" var showPass = false if collectionView == collectionConfirmPin { enteredPin = tfconfirmPin.text ?? "" showPass = self.isShowConfirmPin } if indexPath.row < enteredPin.count { cell.isSelect = true } else { cell.isSelect = false } let charactor = indexPath.row < enteredPin.count ? enteredPin[indexPath.row] : "" cell.showPassword(isShow: showPass, charactorPassword: charactor) return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { collectionConfirmPin.reloadData() } func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if textField == tfconfirmPin { // Handle backspace if string.isEmpty { let currentText = textField.text as NSString? ?? "" _ = currentText.replacingCharacters(in: range, with: string) collectionConfirmPin.reloadData() return true } // Limit the pin length to 6 characters let maxLength = 6 // Check if the new pin length exceeds the limit guard let text = textField.text, (text.count + string.count - range.length) Void) { let rq = VerifyPINRq() rq.pin = pin rq.AccountID = "\(XConfig.Account.ID)" rq.MobileNumber = XConfig.Account.MobileNumber rq.pin = tfconfirmPin.text ?? "" rq.Signature = (rq.AccountID + rq.MobileNumber + rq.pin + XConfig.SALT).sha256() let router = XRouter.verifyPIN(params: XUtilities.ObjToString(rq)) showLoading() XConnection.requestCode(router) { [weak self] code, message in guard let self = self else { return } self.hideLoading() if let code = code, code == "00" { // PIN verification successful completion(true, nil) } else { // PIN verification failed let errorMessage: String if code == "01" { errorMessage = "Mã PIN không chính xác" } else { errorMessage = message ?? "Đã có lỗi xảy ra, vui lòng thực hiện lại." } completion(false, errorMessage) } } } } }
Источник: https://stackoverflow.com/questions/781 ... -verifypin
whenever user entering their pin and then press enter on the num pad no matter if they typed a wrong pin or the textfield is empty, there was no error message show up at the bottom.
However, whenever they pressed the hide/show button, the error work
I want it to show up not depending on the hide/show button
this is my code:
import Foundation import UIKit @objc func editingChanged(_ textField: UITextField) { switch textField { case tfconfirmPin: collectionConfirmPin.reloadData() default: break } } func textFieldShouldReturn(_ textField: UITextField) -> Bool { if textField == tfconfirmPin { tfconfirmPin.resignFirstResponder() onPinEntered() return false } return true } @objc private func onPinEntered() { let pin = tfconfirmPin.text ?? "" // Check if pin is empty before attempting verification guard !pin.isEmpty else { toastBottom("Vui lòng nhập mã pin để xác nhận giao dịch") return } doVerifyPIN(pin: pin) { [weak self] success, errorMessage in guard let self = self else { return } DispatchQueue.main.async { if success { self.pinVerificationDelegate?.didVerifyPINSuccess(pin: pin) self.dismiss(animated: true) } else { // Display "Mã PIN không chính xác" message regardless of the hide/show state self.toastBottom("Mã PIN không chính xác") } } } } @objc private func handleCollectionTap() { tfconfirmPin.becomeFirstResponder() } } extension ConfirmPinDialog { private func addView() { view.addSubview(contentView) contentView.setAnchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0) let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapOutside)) contentView.addGestureRecognizer(tapGestureRecognizer) viewConfirmPin.addSubview(lbTitle) lbTitle.setAnchor(top: viewConfirmPin.topAnchor, left: viewConfirmPin.leftAnchor, bottom: nil, right: viewConfirmPin.rightAnchor, paddingTop: (viewConfirmPin.frame.height - 80) / 2, paddingLeft: 0, paddingBottom: 0, paddingRight: 0) lbTitle.textAlignment = .center lbTitle.textColor = .XPrimaryColor lbTitle.font = .customBoldFont(size: 18) lbTitle.layer.masksToBounds = true lbTitle.backgroundColor = UIColor.white lbTitle.layer.cornerRadius = 7 lbTitle.heightAnchor.constraint(equalToConstant: 60).isActive = true lbTitle.tintColor = .clear lbTitle.frame = viewConfirmPin.bounds contentView.addSubview(viewConfirmPin) viewConfirmPin.setAnchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: view.frame.height * 0.3, paddingLeft: 70, paddingBottom: 30, paddingRight: 70) // Adjusted the left and right padding viewConfirmPin.heightAnchor.constraint(equalToConstant: 60).isActive = true // Adjusted the height viewConfirmPin.backgroundColor = UIColor.white viewConfirmPin.layer.cornerRadius = 7 // Adjusted the corner radius viewConfirmPin.tintColor = .clear // Add collectionConfirmPin viewConfirmPin.addSubview(collectionConfirmPin) collectionConfirmPin.setAnchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 15, paddingLeft: 15, paddingBottom: 0, paddingRight: 0) collectionConfirmPin.dataSource = self collectionConfirmPin.delegate = self collectionConfirmPin.viewHeight(20) collectionConfirmPin.viewWidth(100) collectionConfirmPin.register(EnterPasswordCell.self, forCellWithReuseIdentifier: "EnterPasswordCell") collectionConfirmPin.centerY(toView: viewConfirmPin) collectionConfirmPin.centerX(toView: viewConfirmPin) tfconfirmPin.isUserInteractionEnabled = true collectionConfirmPin.isUserInteractionEnabled = true let collectionTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleCollectionTap)) collectionConfirmPin.addGestureRecognizer(collectionTapGesture) viewConfirmPin.addSubview(tfconfirmPin) tfconfirmPin.setAnchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, height: 40) // Adjusted the height tfconfirmPin.centerX(toView: viewConfirmPin) tfconfirmPin.centerY(toView: viewConfirmPin) tfconfirmPin.tintColor = UIColor.clear tfconfirmPin.delegate = self tfconfirmPin.isSecureTextEntry = !isShowConfirmPin tfconfirmPin.addTarget(self, action: #selector(editingChanged), for: .editingChanged) tfconfirmPin.layer.borderColor = UIColor.lightGray.cgColor tfconfirmPin.layer.borderWidth = 0.1 tfconfirmPin.layer.cornerRadius = 10 tfconfirmPin.layer.masksToBounds = true viewConfirmPin.addSubview(imgconfirmPinEntry) imgconfirmPinEntry.setAnchor(top: nil, left: tfconfirmPin.rightAnchor, bottom: nil, right: viewConfirmPin.rightAnchor, paddingTop: 0, paddingLeft: 15, paddingBottom: 0, paddingRight: 15, width: 40, height: 40) imgconfirmPinEntry.centerY(toView: viewConfirmPin) imgconfirmPinEntry.isUserInteractionEnabled = true imgconfirmPinEntry.addTarget(self, action: #selector(passwordEntry), for: .touchUpInside) imgconfirmPinEntry.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 5) viewConfirmPin.bringSubviewToFront(imgconfirmPinEntry) } @objc private func handleTapOutside() { dismissDialog() } } extension ConfirmPinDialog { @objc func passwordEntry() { isShowConfirmPin.toggle() tfconfirmPin.isSecureTextEntry = !isShowConfirmPin if isShowConfirmPin { imgconfirmPinEntry.setImage(UIImage(named: "ic_show_password"), for: .normal) } else { imgconfirmPinEntry.setImage(UIImage(named: "ic_hide_password"), for: .normal) } collectionConfirmPin.reloadData() } } extension ConfirmPinDialog: UICollectionViewDelegate,UICollectionViewDataSource,UITextFieldDelegate { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 6 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "EnterPasswordCell", for: indexPath) as! EnterPasswordCell var enteredPin = "" var showPass = false if collectionView == collectionConfirmPin { enteredPin = tfconfirmPin.text ?? "" showPass = self.isShowConfirmPin } if indexPath.row < enteredPin.count { cell.isSelect = true } else { cell.isSelect = false } let charactor = indexPath.row < enteredPin.count ? enteredPin[indexPath.row] : "" cell.showPassword(isShow: showPass, charactorPassword: charactor) return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { collectionConfirmPin.reloadData() } func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if textField == tfconfirmPin { // Handle backspace if string.isEmpty { let currentText = textField.text as NSString? ?? "" _ = currentText.replacingCharacters(in: range, with: string) collectionConfirmPin.reloadData() return true } // Limit the pin length to 6 characters let maxLength = 6 // Check if the new pin length exceeds the limit guard let text = textField.text, (text.count + string.count - range.length) Void) { let rq = VerifyPINRq() rq.pin = pin rq.AccountID = "\(XConfig.Account.ID)" rq.MobileNumber = XConfig.Account.MobileNumber rq.pin = tfconfirmPin.text ?? "" rq.Signature = (rq.AccountID + rq.MobileNumber + rq.pin + XConfig.SALT).sha256() let router = XRouter.verifyPIN(params: XUtilities.ObjToString(rq)) showLoading() XConnection.requestCode(router) { [weak self] code, message in guard let self = self else { return } self.hideLoading() if let code = code, code == "00" { // PIN verification successful completion(true, nil) } else { // PIN verification failed let errorMessage: String if code == "01" { errorMessage = "Mã PIN không chính xác" } else { errorMessage = message ?? "Đã có lỗi xảy ra, vui lòng thực hiện lại." } completion(false, errorMessage) } } } } }
Источник: https://stackoverflow.com/questions/781 ... -verifypin
Мобильная версия