Get the current first responder without using a private API

前端 未结 28 1796
夕颜
夕颜 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:49

    It's not pretty, but the way I resign the firstResponder when I don't know what that the responder is:

    Create an UITextField, either in IB or programmatically. Make it Hidden. Link it up to your code if you made it in IB.

    Then, when you want to dismiss the keyboard, you switch the responder to the invisible text field, and immediately resign it:

        [self.invisibleField becomeFirstResponder];
        [self.invisibleField resignFirstResponder];
    
    0 讨论(0)
  • 2020-11-22 05:51

    This is what I have in my UIViewController Category. Useful for many things, including getting first responder. Blocks are great!

    - (UIView*) enumerateAllSubviewsOf: (UIView*) aView UsingBlock: (BOOL (^)( UIView* aView )) aBlock {
    
     for ( UIView* aSubView in aView.subviews ) {
      if( aBlock( aSubView )) {
       return aSubView;
      } else if( ! [ aSubView isKindOfClass: [ UIControl class ]] ){
       UIView* result = [ self enumerateAllSubviewsOf: aSubView UsingBlock: aBlock ];
    
       if( result != nil ) {
        return result;
       }
      }
     }    
    
     return nil;
    }
    
    - (UIView*) enumerateAllSubviewsUsingBlock: (BOOL (^)( UIView* aView )) aBlock {
     return [ self enumerateAllSubviewsOf: self.view UsingBlock: aBlock ];
    }
    
    - (UIView*) findFirstResponder {
     return [ self enumerateAllSubviewsUsingBlock:^BOOL(UIView *aView) {
      if( [ aView isFirstResponder ] ) {
       return YES;
      }
    
      return NO;
     }];
    }
    
    0 讨论(0)
  • 2020-11-22 05:51

    you can call privite api like this ,apple ignore:

    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    SEL sel = NSSelectorFromString(@"firstResponder");
    UIView   *firstResponder = [keyWindow performSelector:sel];
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-11-22 05:53

    Iterate over the views that could be the first responder and use - (BOOL)isFirstResponder to determine if they currently are.

    0 讨论(0)
  • 2020-11-22 05:53

    Code below work.

    - (id)ht_findFirstResponder
    {
        //ignore hit test fail view
        if (self.userInteractionEnabled == NO || self.alpha <= 0.01 || self.hidden == YES) {
            return nil;
        }
        if ([self isKindOfClass:[UIControl class]] && [(UIControl *)self isEnabled] == NO) {
            return nil;
        }
    
        //ignore bound out screen
        if (CGRectIntersectsRect(self.frame, [UIApplication sharedApplication].keyWindow.bounds) == NO) {
            return nil;
        }
    
        if ([self isFirstResponder]) {
            return self;
        }
    
        for (UIView *subView in self.subviews) {
            id result = [subView ht_findFirstResponder];
            if (result) {
                return result;
            }
        }
        return nil;
    }   
    
    0 讨论(0)
提交回复
热议问题