Skip to content

Commit b011340

Browse files
committed
Make all the methods of ICTokenFieldDelegate protocol optional
1 parent 6f30db2 commit b011340

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

Source/ICTokenField.swift

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,27 @@
2626

2727
import UIKit
2828

29-
public protocol ICTokenFieldDelegate: class {
30-
func tokenFieldDidBeginEditing(tokenField: ICTokenField)
31-
func tokenFieldDidEndEditing(tokenField: ICTokenField)
32-
func tokenFieldWillReturn(tokenField: ICTokenField)
33-
func tokenField(tokenField: ICTokenField, didEnterText text: String)
34-
func tokenField(tokenField: ICTokenField, didDeleteText text: String, atIndex index: Int)
29+
/// The protocol defines the messages sent to a delegate. All the methods are optional.
30+
@objc public protocol ICTokenFieldDelegate: NSObjectProtocol {
31+
/// Tells the delegate that editing began for the token field.
32+
optional func tokenFieldDidBeginEditing(tokenField: ICTokenField)
33+
/// Tells the delegate that editing stopped for the token field.
34+
optional func tokenFieldDidEndEditing(tokenField: ICTokenField)
35+
/// Tells the delegate that the token field will process the pressing of the return button.
36+
optional func tokenFieldWillReturn(tokenField: ICTokenField)
37+
/// Tells the delegate that the text becomes a token in the token field.
38+
optional func tokenField(tokenField: ICTokenField, didEnterText text: String)
39+
/// Tells the delegate that the token at certain index is removed from the token field.
40+
optional func tokenField(tokenField: ICTokenField, didDeleteText text: String, atIndex index: Int)
3541
}
3642

43+
3744
////////////////////////////////////////////////////////////////////////////////
3845

3946

4047
public class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDelegate {
4148

49+
/// The receiver’s delegate.
4250
public weak var delegate: ICTokenFieldDelegate?
4351

4452
/// Characters that completes a new token, defaults are whitespace and commas.
@@ -213,14 +221,14 @@ public class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDele
213221
}
214222

215223
public func textFieldDidBeginEditing(textField: UITextField) {
216-
delegate?.tokenFieldDidBeginEditing(self)
224+
delegate?.tokenFieldDidBeginEditing?(self)
217225
}
218226

219227
public func textFieldDidEndEditing(textField: UITextField) {
220228
completeCurrentInputText()
221229
togglePlaceholderIfNeeded()
222230
tokens.forEach { $0.highlighted = false }
223-
delegate?.tokenFieldDidEndEditing(self)
231+
delegate?.tokenFieldDidEndEditing?(self)
224232
}
225233

226234
public func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
@@ -243,7 +251,7 @@ public class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDele
243251
if newToken != delimiter {
244252
tokens.append(ICToken(text: newToken, normalAttributes: normalTokenAttributes, highlightedAttributes: highlightedTokenAttributes))
245253
layoutTokenTextField()
246-
delegate?.tokenField(self, didEnterText: newToken)
254+
delegate?.tokenField?(self, didEnterText: newToken)
247255
}
248256
togglePlaceholderIfNeeded()
249257

@@ -256,7 +264,7 @@ public class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDele
256264
public func textFieldShouldReturn(textField: UITextField) -> Bool {
257265
completeCurrentInputText()
258266
togglePlaceholderIfNeeded()
259-
delegate?.tokenFieldWillReturn(self)
267+
delegate?.tokenFieldWillReturn?(self)
260268
return true
261269
}
262270

@@ -320,7 +328,7 @@ public class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDele
320328
layoutTokenTextField()
321329
togglePlaceholderIfNeeded()
322330
inputTextField.showsCursor = true
323-
delegate?.tokenField(self, didDeleteText: token.text, atIndex: index)
331+
delegate?.tokenField?(self, didDeleteText: token.text, atIndex: index)
324332
return true
325333
}
326334
}
@@ -389,7 +397,7 @@ public class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDele
389397
inputTextField.text = nil
390398
tokens.append(ICToken(text: text, normalAttributes: normalTokenAttributes, highlightedAttributes: highlightedTokenAttributes))
391399
layoutTokenTextField()
392-
delegate?.tokenField(self, didEnterText: text)
400+
delegate?.tokenField?(self, didEnterText: text)
393401
}
394402

395403
/// Removes the input text and all displayed tokens.

0 commit comments

Comments
 (0)