From 87f058a5bd89e40b83a4b7995b7035ca805643c3 Mon Sep 17 00:00:00 2001 From: Quan Date: Fri, 14 Jan 2022 08:00:41 +0700 Subject: [PATCH] Add text field text changed handler --- Source/SwiftAlertView.swift | 50 ++++++++++++++++++----- SwiftAlertView/ViewController.swift | 61 ++++++++++++++++------------- 2 files changed, 73 insertions(+), 38 deletions(-) diff --git a/Source/SwiftAlertView.swift b/Source/SwiftAlertView.swift index 597117f..ba2d7d9 100644 --- a/Source/SwiftAlertView.swift +++ b/Source/SwiftAlertView.swift @@ -59,7 +59,18 @@ public class SwiftAlertView: UIView { public var backgroundImage: UIImage? // public var backgroundColor: UIColor? // inherits from UIView - public var cancelButtonIndex = 0 // default is 0, set this property if you want to change the position of cancel button + public var cancelButtonIndex = 0 { // default is 0, set this property if you want to change the position of cancel button + didSet { + for i in 0.. Void)? // all buttons - public var onCancelClicked: (() -> Void)? // for cancel button - public var onActionButtonClicked: ((_ buttonIndex: Int) -> (Void))? // sometimes you want to handle the action button clicked event but don't want to write if/else in onButtonClicked closure, use this property - - + // MARK: Constants private let kSeparatorWidth: CGFloat = 0.5 @@ -133,6 +139,11 @@ public class SwiftAlertView: UIView { private var viewWidth: CGFloat = 0 private var viewHeight: CGFloat = 0 private var isMoveUpWithKeyboard = false + + private var onButtonClicked: ((_ buttonIndex: Int) -> Void)? + private var onCancelClicked: (() -> Void)? + private var onActionButtonClicked: ((_ buttonIndex: Int) -> (Void))? + private var onTextChanged: ((_ text: String?, _ textFieldIndex: Int) -> Void)? // MARK: Initialization @@ -202,6 +213,8 @@ public class SwiftAlertView: UIView { textField.font = .systemFont(ofSize: 14) textField.borderStyle = .roundedRect textField.delegate = self + textField.tag = textFields.count + textField.addTarget(self, action: #selector(textChanged(_:)), for: .editingChanged) configurationHandler?(textField) textFields.append(textField) addSubview(textField) @@ -337,17 +350,29 @@ public class SwiftAlertView: UIView { } } - // handle button click events - public func onButtonClicked(_ handler: @escaping (_ alertView: SwiftAlertView, _ buttonIndex: Int) -> Void) { + // handle events + @discardableResult + public func onButtonClicked(_ handler: @escaping (_ alertView: SwiftAlertView, _ buttonIndex: Int) -> Void) -> SwiftAlertView { self.onButtonClicked = { index in handler(self, index) } + return self } - public func onActionButtonClicked(_ handler: @escaping (_ alertView: SwiftAlertView, _ buttonIndex: Int) -> Void) { + @discardableResult + public func onActionButtonClicked(_ handler: @escaping (_ alertView: SwiftAlertView, _ buttonIndex: Int) -> Void) -> SwiftAlertView { self.onActionButtonClicked = { index in handler(self, index) } + return self + } + + @discardableResult + public func onTextChanged(_ handler: @escaping (_ alertView: SwiftAlertView, _ text: String?, _ textFieldIndex: Int) -> Void) -> SwiftAlertView { + self.onTextChanged = { text, index in + handler(self, text, index) + } + return self } } @@ -613,6 +638,11 @@ extension SwiftAlertView { } } + @objc private func textChanged(_ textField: UITextField) { + let index = textField.tag + onTextChanged?(textField.text, index) + } + // MARK: Utils diff --git a/SwiftAlertView/ViewController.swift b/SwiftAlertView/ViewController.swift index 980dd82..82eb426 100644 --- a/SwiftAlertView/ViewController.swift +++ b/SwiftAlertView/ViewController.swift @@ -11,8 +11,8 @@ import UIKit final class ViewController: UITableViewController { let demoTitles: [String] = ["Dark Mode", - "More Than Two Buttons", - "Add Text Fields", + "Alert with Text Field", + "More Text Fields & Validation Label", "Customize Font & Color", "Custom Content View", "Init From Nib File", @@ -30,33 +30,30 @@ final class ViewController: UITableViewController { switch indexPath.row { case 0: - let alertView = SwiftAlertView(title: "Lorem ipsum", - message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ", - buttonTitles: "Cancel", "Ok") - alertView.style = .dark - alertView.onButtonClicked = { buttonIndex in + SwiftAlertView.show(title: "Lorem ipsum", + message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ", + buttonTitles: "Cancel", "Ok") { + $0.style = .dark + } + .onButtonClicked { _, buttonIndex in print("Button Clicked At Index \(buttonIndex)") } - - alertView.onCancelClicked = { - print("Cancel Button Clicked") + + case 1: + SwiftAlertView.show(title: "Title", + message: "Message", + buttonTitles: "Button 1", "Button 2", "Button 3", "Cancel") { alertView in + alertView.style = .auto + alertView.cancelButtonIndex = 3 + alertView.buttonTitleColor = UIColor(red: 0.8764, green: 0.5, blue: 0.3352, alpha: 1) + alertView.addTextField { textField in + textField.placeholder = "Placeholder" + } } - - alertView.onActionButtonClicked = { _ in - print("Action Button Clicked") + .onActionButtonClicked { alert, buttonIndex in + let text = alert.textField(at: 0)?.text ?? "" + print("Text: ", text) } - - alertView.show() - - case 1: - let alertView = SwiftAlertView(title: "Lorem ipsum", - message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ", - buttonTitles: "Button 1", "Button 2", "Button 3", "Cancel") - alertView.style = .auto - alertView.delegate = self - alertView.cancelButtonIndex = 3 - alertView.buttonTitleColor = UIColor(red: 0.8764, green: 0.5, blue: 0.3352, alpha: 1) - alertView.show() case 2: SwiftAlertView.show(title: "Sign in", buttonTitles: "Cancel", "Sign In") { alertView in @@ -71,8 +68,16 @@ final class ViewController: UITableViewController { } .onActionButtonClicked { alert, buttonIndex in let username = alert.textField(at: 0)?.text ?? "" - print("Username: ", username) - alert.validationLabel.text = "Username is incorrect" + if username.isEmpty { + alert.validationLabel.text = "Username is incorrect" + } else { + alert.dismiss() + } + } + .onTextChanged { _, text, index in + if index == 0 { + print("Username text changed: ", text ?? "") + } } case 3: @@ -89,7 +94,7 @@ final class ViewController: UITableViewController { alertView.button(at: 1)?.setTitleColor(.purple, for: .normal) alertView.button(at: 0)?.titleLabel?.font = UIFont(name: "Marker Felt", size: 20) alertView.button(at: 1)?.titleLabel?.font = UIFont(name: "Marker Felt", size: 20) - + alertView.delegate = self alertView.show() case 4: