Created
March 1, 2017 09:28
-
-
Save edwardean/bb43de276619a7f6d65b68c3159f2e72 to your computer and use it in GitHub Desktop.
iOS Get FirstResponder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ////////////////////////////////////////////////// | |
| @implementation UIView (FirstResponder) | |
| - (UIView *)findFirstResponder { | |
| if (self.isFirstResponder) { | |
| return self; | |
| } | |
| for (UIView *subView in self.subviews) { | |
| UIView *firstResponder = [subView findFirstResponder]; | |
| if (firstResponder != nil) { | |
| return firstResponder; | |
| } | |
| } | |
| return nil; | |
| } | |
| @end | |
| ////////////////////////////////////////////////// | |
| import UIKit | |
| private weak var _currentFirstResonder: AnyObject? | |
| extension UIResponder { | |
| static func findFirstResponder() -> AnyObject? { | |
| _currentFirstResonder = nil | |
| UIApplication.shared.sendAction(#selector(UIResponder._findFirstResponder(_:)), to: nil, from: nil, for: nil) | |
| return _currentFirstResonder | |
| } | |
| @objc private func _findFirstResponder(_ sender: AnyObject?) { | |
| _currentFirstResonder = self | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should be
_currentFirstResponder, not_currentFirstResonder