Skip to content

Commit

Permalink
Fix UIResponder.valid since it was looking at the wrong target
Browse files Browse the repository at this point in the history
  • Loading branch information
vincode-io committed Aug 18, 2024
1 parent b37dab5 commit adab868
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion VinUtility/Sources/VinUtility/UIResponder+VU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import UIKit
extension UIResponder {

private weak static var _currentFirstResponder: UIResponder? = nil
private weak static var _currentTopResponder: UIResponder? = nil

@available(iOSApplicationExtension, unavailable)
public static var isFirstResponderTextField: Bool {
Expand All @@ -27,6 +28,13 @@ extension UIResponder {
return UIResponder._currentFirstResponder
}

@available(iOSApplicationExtension, unavailable)
internal static var currentTopResponder: UIResponder? {
UIResponder._currentTopResponder = nil
UIApplication.shared.sendAction(#selector(findTopResponder(sender:)), to: nil, from: nil, for: nil)
return UIResponder._currentTopResponder
}

@available(iOSApplicationExtension, unavailable)
public static func resignCurrentFirstResponder() {
if let responder = currentFirstResponder {
Expand All @@ -35,7 +43,7 @@ extension UIResponder {
}

public static func valid(action: Selector) -> Bool {
return Self.currentFirstResponder?.target(forAction: action, withSender: nil) != nil
return Self.currentTopResponder?.target(forAction: action, withSender: nil) != nil
}

@objc internal func findFirstResponder(sender: AnyObject) {
Expand All @@ -44,5 +52,8 @@ extension UIResponder {
}
}

@objc internal func findTopResponder(sender: AnyObject) {
UIResponder._currentTopResponder = self
}
}
#endif

0 comments on commit adab868

Please sign in to comment.