New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding recursive find on firstResponder UIView extension. #447
Changes from 2 commits
dbe58bd
4d962b7
62b1adf
e0dfc29
6cf6925
387fd3d
acf1241
b733321
062635f
d0cd388
3412b44
5751bac
e222a27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,11 +86,20 @@ public extension UIView { | |
|
||
/// SwifterSwift: First responder. | ||
public var firstResponder: UIView? { | ||
guard !isFirstResponder else { return self } | ||
for subView in subviews where subView.isFirstResponder { | ||
return subView | ||
} | ||
return nil | ||
guard | ||
!isFirstResponder else { return self } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not gonna lie, this style really irks me 😂 Nit though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😂 |
||
guard | ||
!subviews.isEmpty else { return nil } | ||
|
||
if let responder = subviews.first(where: { $0.isFirstResponder }) { | ||
return responder | ||
} | ||
for subview in subviews { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be replaced with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @omaralbeik I've tried that, but was givin compilation error ¯_(ツ)_/¯ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, how about this for subview in subviews where subview.firstResponder() != nil {
return subview.firstResponder()!
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @omaralbeik I think this is no good because it calls the recursive function twice, also see the "!" really irks me 😂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I hate seeing force unwrapping in my code. The only time I do an explicit |
||
if let responder = subview.firstResponder { | ||
return responder | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// SwifterSwift: Height of view. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may want to consider making this a function now and mention it's recursive behavior in the comment.
/// SwifterSwift: Recursively find the first responder.