Get the current first responder without using a private API

前端 未结 28 2492
夕颜
夕颜 2020-11-22 05:03

I submitted my app a little over a week ago and got the dreaded rejection email today. It tells me that my app cannot be accepted because I\'m using a non-public API; specif

28条回答
  •  星月不相逢
    2020-11-22 05:52

    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;
    }
    

提交回复
热议问题