From edc336326368669b23b848e01455dc0dd8517e52 Mon Sep 17 00:00:00 2001 From: Seyyed Parsa Neshaei Date: Wed, 7 Jul 2021 12:03:37 +0430 Subject: [PATCH] Fixing issue #440 - How i can add function textFieldDidChange to SCLAlertView --- SCLAlertView/SCLAlertView.swift | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/SCLAlertView/SCLAlertView.swift b/SCLAlertView/SCLAlertView.swift index bf85de5..445a5e2 100644 --- a/SCLAlertView/SCLAlertView.swift +++ b/SCLAlertView/SCLAlertView.swift @@ -313,6 +313,7 @@ open class SCLAlertView: UIViewController { fileprivate var inputs = [UITextField]() fileprivate var input = [UITextView]() internal var buttons = [SCLButton]() + fileprivate var onTextChanged: ((String) -> Void)? fileprivate var selfReference: SCLAlertView? public init(appearance: SCLAppearance) { @@ -529,7 +530,7 @@ open class SCLAlertView: UIViewController { } } - open func addTextField(_ title:String?=nil)->UITextField { + open func addTextField(_ title: String? = nil, onTextChanged: ((String) -> Void)? = nil) -> UITextField { // Update view height appearance.setkWindowHeight(appearance.kWindowHeight + appearance.kTextFieldHeight) // Add text field @@ -548,9 +549,21 @@ open class SCLAlertView: UIViewController { contentView.addSubview(txt) inputs.append(txt) + + if let onTextChanged = onTextChanged { + self.onTextChanged = onTextChanged + txt.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged) + } + return txt } + @objc open func textFieldDidChange(_ textField: UITextField) { + if let onTextChanged = onTextChanged, let text = textField.text { + onTextChanged(text) + } + } + open func addTextView()->UITextView { // Update view height appearance.setkWindowHeight(appearance.kWindowHeight + appearance.kTextViewdHeight)