Get the current first responder without using a private API

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

    Using Swift and with a specific UIView object this might help:

    func findFirstResponder(inView view: UIView) -> UIView? {
        for subView in view.subviews as! [UIView] {
            if subView.isFirstResponder() {
                return subView
            }
            
            if let recursiveSubView = self.findFirstResponder(inView: subView) {
                return recursiveSubView
            }
        }
        
        return nil
    }
    

    Just place it in your UIViewController and use it like this:

    let firstResponder = self.findFirstResponder(inView: self.view)
    

    Take note that the result is an Optional value so it will be nil in case no firstResponder was found in the given views subview hierarchy.

提交回复
热议问题