@@ -11,11 +11,30 @@ import Foundation
1111// MARK: Protocols
1212
1313/// Delegate protocol to handle form submitting
14- public protocol ValidatedFormKeyboardDelegate {
14+ public protocol FormDelegate {
1515 /**
1616 Triggered when the keybiard return key is touched on the last field.
1717 */
1818 func goReturnKeyTouched( )
19+
20+ /*
21+ Returns the first input of a form
22+
23+ - Parameter form: The form
24+
25+ - Return: The first input.
26+ */
27+ func getFirstInput( form: Form ) -> FormInput
28+
29+ /*
30+ Returns the following input of a form input
31+
32+ - Parameter form: The form
33+ - Parameter currentInput: The current input
34+
35+ - Return: If the current input is the last one, nil. If not, the following input.
36+ */
37+ func getNextInput( form: Form , currentInput: FormInput ) -> FormInput ?
1938}
2039
2140// MARK: Class
@@ -32,7 +51,10 @@ public class Form: UIScrollView {
3251 var keyboardViewHeight : CGFloat = 216
3352
3453 /// The stored delegate
35- public var keyboardDelegate : ValidatedFormKeyboardDelegate !
54+ public var formDelegate : FormDelegate !
55+
56+ /// The current input which has been focused
57+ private var currentInput : FormInput !
3658
3759 /// The form inputs
3860 public var inputs : [ FormInput ] = [ ] {
@@ -80,6 +102,16 @@ public class Form: UIScrollView {
80102 name: tfReturnedNotifName,
81103 object: nil
82104 )
105+
106+ NSNotificationCenter . defaultCenter ( ) . addObserver (
107+ self ,
108+ selector: #selector( Form . textFieldBecameFirstResponder ( _: ) ) ,
109+ name: tfBecameFirstResponderNotifName,
110+ object: nil
111+ )
112+ if let _ = formDelegate {
113+ currentInput = formDelegate. getFirstInput ( self )
114+ }
83115 }
84116
85117 /**
@@ -137,15 +169,15 @@ public class Form: UIScrollView {
137169 */
138170 func textFieldReturnedFired( notification: NSNotification ) {
139171 if let textfield = notification. object as? FormInput {
140- if let index : Int = indexForInput ( textfield) {
141- if isLastInput ( textfield) {
142- textfield . stopEditing ( )
143- resetScrollingZone ( )
144- if let _ = keyboardDelegate {
145- keyboardDelegate . goReturnKeyTouched ( )
146- }
147- } else {
148- inputs [ index + 1 ] . becomeFirstResponder ( )
172+ if isLastInput ( textfield) {
173+ textfield. stopEditing ( )
174+ resetScrollingZone ( )
175+ if let _ = formDelegate {
176+ formDelegate . goReturnKeyTouched ( )
177+ }
178+ } else {
179+ if let _ = formDelegate {
180+ formDelegate . getNextInput ( self , currentInput : currentInput ) ? . becomeFirstResponder ( )
149181 }
150182 }
151183 }
@@ -167,21 +199,29 @@ public class Form: UIScrollView {
167199 }
168200
169201 /**
170- Checks if the given input is the last one .
202+ Stores the current textfield .
171203
172- - Parameter input : the input to compare
204+ - Parameter notification : the received notification
173205 */
174- private func isLastInput( input: FormInput ) -> Bool {
175- return input == inputs. last
206+ func textFieldBecameFirstResponder( notification: NSNotification ) {
207+ if let textfield = notification. object as? FormInput {
208+ currentInput = textfield
209+ }
176210 }
177211
178212 /**
179- Gives the index of a given input
213+ Checks if the given input is the last one.
180214
181- - Parameter input: the input to get the index.
215+ - Parameter input: the input to compare
182216 */
183- private func indexForInput( input: FormInput ) -> Int ? {
184- return inputs. indexOf ( input)
217+ private func isLastInput( input: FormInput ) -> Bool {
218+ if let _ = formDelegate {
219+ if let nextInput: FormInput = formDelegate. getNextInput ( self , currentInput: currentInput) {
220+ return false
221+ }
222+ }
223+
224+ return true
185225 }
186226}
187227
0 commit comments