Get the current first responder without using a private API

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

    Here's a category that allows you to quickly find the first responder by calling [UIResponder currentFirstResponder]. Just add the following two files to your project:

    UIResponder+FirstResponder.h

    #import 
    @interface UIResponder (FirstResponder)
        +(id)currentFirstResponder;
    @end
    

    UIResponder+FirstResponder.m

    #import "UIResponder+FirstResponder.h"
    static __weak id currentFirstResponder;
    @implementation UIResponder (FirstResponder)
        +(id)currentFirstResponder {
             currentFirstResponder = nil;
             [[UIApplication sharedApplication] sendAction:@selector(findFirstResponder:) to:nil from:nil forEvent:nil];
             return currentFirstResponder;
        }
        -(void)findFirstResponder:(id)sender {
            currentFirstResponder = self;
        }
    @end
    

    The trick here is that sending an action to nil sends it to the first responder.

    (I originally published this answer here: https://stackoverflow.com/a/14135456/322427)

提交回复
热议问题