Skip to content

Commit

Permalink
Fixed intermediate editing false issue
Browse files Browse the repository at this point in the history
  • Loading branch information
varun-pm committed Jul 17, 2018
1 parent 40c6faa commit 811c8f4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Added support for error border color when invalid OTP is entered.

Fixed delete backward issue when field is empty.

### Version 1.1.4

Fixed intermediate editing false issue.


## License
MIT License
Expand Down
20 changes: 9 additions & 11 deletions VPMOTPView/VPMOTPView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,19 @@ class VPMOTPView: UIView {

//MARK: Public functions
/// Call this method to create the OTP field view. This method should be called at the last after necessary customization needed. If any property is modified at a later stage is simply ignored.
func initalizeUI() {
func initializeUI() {
self.layer.masksToBounds = true
self.layoutIfNeeded()

initalizeOTPFields()
initializeOTPFields()

// Forcefully try to make first otp field as first responder
(viewWithTag(1) as? VPMOTPTextField)?.becomeFirstResponder()
}

//MARK: Private functions
// Set up the fields
fileprivate func initalizeOTPFields() {
fileprivate func initializeOTPFields() {
secureEntryData.removeAll()

for index in stride(from: 0, to: otpFieldsCount, by: 1) {
Expand Down Expand Up @@ -210,22 +210,20 @@ class VPMOTPView: UIView {
var isTextFilled = true
var nextOTPField: UITextField?

// If intermediate editing is not allowed, then check for last filled from the current field in forward direction.
// If intermediate editing is not allowed, then check for last filled field in forward direction.
if !shouldAllowIntermediateEditing {
for index in stride(from: textField.tag + 1, to: otpFieldsCount + 1, by: 1) {
for index in stride(from: 1, to: otpFieldsCount + 1, by: 1) {
let tempNextOTPField = viewWithTag(index) as? UITextField

if let tempNextOTPFieldText = tempNextOTPField?.text, !tempNextOTPFieldText.isEmpty {
if let tempNextOTPFieldText = tempNextOTPField?.text, tempNextOTPFieldText.isEmpty {
nextOTPField = tempNextOTPField

break
}
}

if let nextOTPField = nextOTPField {
if nextOTPField != textField {
nextOTPField.becomeFirstResponder()
}

isTextFilled = false
isTextFilled = (nextOTPField == textField || (textField.tag) == (nextOTPField.tag - 1))
}
}

Expand Down
3 changes: 2 additions & 1 deletion VPMOTPViewExample/VPMOTPViewExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ class ViewController: UIViewController {
otpView.otpFieldErrorBorderColor = UIColor.red
otpView.otpFieldBorderWidth = 2
otpView.delegate = self
otpView.shouldAllowIntermediateEditing = false

// Create the UI
otpView.initalizeUI()
otpView.initializeUI()
}

override func didReceiveMemoryWarning() {
Expand Down

0 comments on commit 811c8f4

Please sign in to comment.