Get the current first responder without using a private API

前端 未结 28 2537
夕颜
夕颜 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

    A common way of manipulating the first responder is to use nil targeted actions. This is a way of sending an arbitrary message to the responder chain (starting with the first responder), and continuing down the chain until someone responds to the message (has implemented a method matching the selector).

    For the case of dismissing the keyboard, this is the most effective way that will work no matter which window or view is first responder:

    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    

    This should be more effective than even [self.view.window endEditing:YES].

    (Thanks to BigZaphod for reminding me of the concept)

提交回复
热议问题