Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
corrected code
Source Link
Daniel Klöck
  • 21.2k
  • 10
  • 98
  • 160

This is good candidate for recursion! No need to add a category to UIView.

Usage (from your view controller):

UIView *firstResponder = [self findFirstResponder:[self view]]; 

Code:

// This is a recursive function - (UIView *)findFirstResponder:(UIView *)view { if ([view isFirstResponder]) return view; // Base case for (UIView *subView in [view subviews]) { if ([self findFirstResponder:subView]) return firstResponder;subView; // Recursion } return nil; } 

This is good candidate for recursion! No need to add a category to UIView.

Usage (from your view controller):

UIView *firstResponder = [self findFirstResponder:[self view]]; 

Code:

// This is a recursive function - (UIView *)findFirstResponder:(UIView *)view { if ([view isFirstResponder]) return view; // Base case for (UIView *subView in [view subviews]) { if ([self findFirstResponder:subView]) return firstResponder; // Recursion } return nil; } 

This is good candidate for recursion! No need to add a category to UIView.

Usage (from your view controller):

UIView *firstResponder = [self findFirstResponder:[self view]]; 

Code:

// This is a recursive function - (UIView *)findFirstResponder:(UIView *)view { if ([view isFirstResponder]) return view; // Base case for (UIView *subView in [view subviews]) { if ([self findFirstResponder:subView]) return subView; // Recursion } return nil; } 
Source Link

This is good candidate for recursion! No need to add a category to UIView.

Usage (from your view controller):

UIView *firstResponder = [self findFirstResponder:[self view]]; 

Code:

// This is a recursive function - (UIView *)findFirstResponder:(UIView *)view { if ([view isFirstResponder]) return view; // Base case for (UIView *subView in [view subviews]) { if ([self findFirstResponder:subView]) return firstResponder; // Recursion } return nil; }