Skip to content

Commit

Permalink
Added a delegate to get a callback when the fields change
Browse files Browse the repository at this point in the history
  • Loading branch information
Fawxy committed May 3, 2017
1 parent de298a8 commit f890174
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 133 deletions.
2 changes: 1 addition & 1 deletion CBPinEntryView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'CBPinEntryView'
s.version = '1.2.0'
s.version = '1.3.0'
s.summary = 'A view for entering arbitrary length numerical pins or codes written in Swift 3.0.'

# This description is used to generate tags and improve search results.
Expand Down
12 changes: 12 additions & 0 deletions CBPinEntryView/Classes/CBPinEntryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

import UIKit

public protocol CBPinEntryViewDelegate {
func entryChanged(_ completed: Bool)
}

@IBDesignable open class CBPinEntryView: UIView {

@IBInspectable var length: Int = CBPinEntryViewDefaults.length
Expand All @@ -31,6 +35,8 @@ import UIKit

fileprivate var entryButtons: [UIButton] = [UIButton]()

public var delegate: CBPinEntryViewDelegate?

override public init(frame: CGRect) {
super.init(frame: frame)

Expand Down Expand Up @@ -73,6 +79,7 @@ import UIKit
textField = UITextField(frame: bounds)
textField.delegate = self
textField.keyboardType = .numberPad
textField.addTarget(self, action: #selector(textfieldChanged(_:)), for: .editingChanged)

self.addSubview(textField)

Expand Down Expand Up @@ -145,6 +152,11 @@ import UIKit
}

extension CBPinEntryView: UITextFieldDelegate {
func textfieldChanged(_ textField: UITextField) {
let complete: Bool = textField.text!.characters.count == length
delegate?.entryChanged(complete)
}

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
errorMode = false
for button in entryButtons {
Expand Down
4 changes: 2 additions & 2 deletions Example/CBPinEntryView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
79DE4521C13BBA2A231E43EC /* [CP] Embed Pods Frameworks */ = {
Expand Down Expand Up @@ -351,7 +351,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */
Expand Down
14 changes: 13 additions & 1 deletion Example/CBPinEntryView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import CBPinEntryView

class ViewController: UIViewController {

@IBOutlet var pinEntryView: CBPinEntryView!
@IBOutlet var pinEntryView: CBPinEntryView! {
didSet {
pinEntryView.delegate = self
}
}
@IBOutlet var stringOutputLabel: UILabel!

@IBAction func pressedButton(_ sender: UIButton) {
Expand All @@ -20,3 +24,11 @@ class ViewController: UIViewController {
}
}

extension ViewController: CBPinEntryViewDelegate {
func entryChanged(_ completed: Bool) {
if completed {
print(pinEntryView.getPinAsString())
}
}
}

6 changes: 3 additions & 3 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- CBPinEntryView (0.1.0)
- CBPinEntryView (1.2.0)

DEPENDENCIES:
- CBPinEntryView (from `../`)
Expand All @@ -9,8 +9,8 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
CBPinEntryView: 105d35d5a9ab1836f6c87d336fafa7002aecf5d5
CBPinEntryView: 4598ecd231c29581d8bcc3e933bee2b866d829bd

PODFILE CHECKSUM: 7a55927a68b2086774c487cb21b7cb4d074c5945

COCOAPODS: 1.2.0
COCOAPODS: 1.2.1
13 changes: 7 additions & 6 deletions Example/Pods/Local Podspecs/CBPinEntryView.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f890174

Please sign in to comment.