Get the current first responder without using a private API

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

    Simplest way to find first responder:

    func sendAction(_ action: Selector, to target: Any?, from sender: Any?, for event: UIEvent?) -> Bool
    

    The default implementation dispatches the action method to the given target object or, if no target is specified, to the first responder.

    Next step:

    extension UIResponder
    {
        private weak static var first: UIResponder? = nil
    
        @objc
        private func firstResponderWhereYouAre(sender: AnyObject)
        {
            UIResponder.first = self
        }
    
        static var actualFirst: UIResponder?
        {
            UIApplication.shared.sendAction(#selector(findFirstResponder(sender:)), to: nil, from: nil, for: nil)
            return UIResponder.first
        }
    }
    

    Usage: Just get UIResponder.actualFirst for your own purposes.

提交回复
热议问题