Skip to content

Commit cf6b6ae

Browse files
committed
Change how errors work, clear error on resign, add new functionality to clear the view
1 parent eac440c commit cf6b6ae

File tree

5 files changed

+55
-14
lines changed

5 files changed

+55
-14
lines changed

CBPinEntryView.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'CBPinEntryView'
11-
s.version = '1.5.3'
11+
s.version = '1.5.4'
1212
s.summary = 'A view for entering arbitrary length numerical pins or codes written in Swift 4.0.'
1313

1414
# This description is used to generate tags and improve search results.

CBPinEntryView/Classes/CBPinEntryView.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public protocol CBPinEntryViewDelegate: class {
8585
private var stackView: UIStackView?
8686
private var textField: UITextField!
8787

88-
fileprivate var errorMode: Bool = false
88+
open var errorMode: Bool = false
8989

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

@@ -195,19 +195,31 @@ public protocol CBPinEntryViewDelegate: class {
195195
textField.becomeFirstResponder()
196196
}
197197

198-
open func toggleError() {
199-
if !errorMode {
198+
open func setError(isError: Bool) {
199+
if isError {
200+
errorMode = true
200201
for button in entryButtons {
201202
button.layer.borderColor = entryErrorBorderColour.cgColor
202203
button.layer.borderWidth = entryBorderWidth
203204
}
204205
} else {
206+
errorMode = false
205207
for button in entryButtons {
206208
button.layer.borderColor = entryBorderColour.cgColor
207209
}
208210
}
211+
}
212+
213+
open func clearEntry() {
214+
setError(isError: false)
215+
textField.text = ""
216+
for button in entryButtons {
217+
button.setTitle("", for: .normal)
218+
}
209219

210-
errorMode = !errorMode
220+
if let firstButton = entryButtons.first {
221+
didPressCodeButton(firstButton)
222+
}
211223
}
212224

213225
open func getPinAsInt() -> Int? {
@@ -235,9 +247,7 @@ public protocol CBPinEntryViewDelegate: class {
235247
@discardableResult open override func resignFirstResponder() -> Bool {
236248
super.resignFirstResponder()
237249

238-
entryButtons.forEach {
239-
$0.layer.borderColor = entryDefaultBorderColour.cgColor
240-
}
250+
setError(isError: false)
241251

242252
return textField.resignFirstResponder()
243253
}

Example/.DS_Store

0 Bytes
Binary file not shown.

Example/CBPinEntryView/Base.lproj/Main.storyboard

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
33
<device id="retina4_0" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
99
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
1010
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1111
</dependencies>
@@ -66,21 +66,39 @@
6666
</constraints>
6767
</stackView>
6868
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="e7G-Qq-ufL">
69-
<rect key="frame" x="141" y="252.5" width="38" height="30"/>
70-
<state key="normal" title="Press"/>
69+
<rect key="frame" x="136" y="252.5" width="49" height="30"/>
70+
<state key="normal" title="Submit"/>
7171
<connections>
72-
<action selector="pressedButton:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="ohr-U9-pHP"/>
72+
<action selector="pressedGetCode:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="uAS-Zl-ovd"/>
73+
</connections>
74+
</button>
75+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="qcc-XO-4s7">
76+
<rect key="frame" x="118" y="302.5" width="84" height="30"/>
77+
<state key="normal" title="Toggle error"/>
78+
<connections>
79+
<action selector="toggleError:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="CMR-iR-lYp"/>
80+
</connections>
81+
</button>
82+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Gkp-B4-Rf5">
83+
<rect key="frame" x="142" y="352.5" width="36" height="30"/>
84+
<state key="normal" title="Clear"/>
85+
<connections>
86+
<action selector="pressedClear:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="s4X-QJ-IXz"/>
7387
</connections>
7488
</button>
7589
</subviews>
7690
<color key="backgroundColor" red="0.94509803920000002" green="0.96078431369999995" blue="0.97254901959999995" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
7791
<constraints>
92+
<constraint firstItem="qcc-XO-4s7" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="Cj4-HF-I59"/>
7893
<constraint firstItem="l00-Oi-KOV" firstAttribute="top" secondItem="8mn-8t-3Ic" secondAttribute="bottom" constant="30" id="Fto-dd-m9g"/>
94+
<constraint firstItem="Gkp-B4-Rf5" firstAttribute="top" secondItem="qcc-XO-4s7" secondAttribute="bottom" constant="20" id="Tct-Nx-GvP"/>
7995
<constraint firstItem="8mn-8t-3Ic" firstAttribute="centerY" secondItem="kh9-bI-dsS" secondAttribute="centerY" multiplier="0.5" id="e15-Jp-UgT"/>
8096
<constraint firstItem="l00-Oi-KOV" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="g8I-HD-8da"/>
8197
<constraint firstItem="e7G-Qq-ufL" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="hkh-Ok-IeY"/>
8298
<constraint firstItem="8mn-8t-3Ic" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="kSo-Zi-uTG"/>
99+
<constraint firstItem="Gkp-B4-Rf5" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="pKl-BL-O1c"/>
83100
<constraint firstItem="e7G-Qq-ufL" firstAttribute="top" secondItem="l00-Oi-KOV" secondAttribute="bottom" constant="40" id="rQd-Pj-ZOf"/>
101+
<constraint firstItem="qcc-XO-4s7" firstAttribute="top" secondItem="e7G-Qq-ufL" secondAttribute="bottom" constant="20" id="tyB-mB-qJl"/>
84102
</constraints>
85103
</view>
86104
<connections>

Example/CBPinEntryView/ViewController.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,22 @@ class ViewController: UIViewController {
1818
}
1919
@IBOutlet var stringOutputLabel: UILabel!
2020

21-
@IBAction func pressedButton(_ sender: UIButton) {
21+
@IBAction func pressedGetCode(_ sender: UIButton) {
2222
stringOutputLabel.text = pinEntryView.getPinAsString()
2323
print(pinEntryView.getPinAsInt() ?? "Nothing entered")
24+
pinEntryView.resignFirstResponder()
25+
}
26+
27+
@IBAction func toggleError(_ sender: UIButton) {
28+
if !pinEntryView.errorMode {
29+
pinEntryView.setError(isError: true)
30+
} else {
31+
pinEntryView.setError(isError: false)
32+
}
33+
}
34+
35+
@IBAction func pressedClear(_ sender: UIButton) {
36+
pinEntryView.clearEntry()
2437
}
2538
}
2639

0 commit comments

Comments
 (0)