Question

If I am currently not having any interaction with the app (no textfield selected etc...), is there still a first responder object? As I tested, if I send a message to the responder chain, then the current ViewController.view receives it, but its isFirstResponder returns with NO. So I am guessing some other element is still first responder? Or there is always a responder chain, but not necessarily a first responder object?

Was it helpful?

Solution 2

The answer:

No, there does not have to be a first responder at all times. You can find routines on git to walk every subview from self.view, looking for the first responder, if you care to:

@implementation UIView (FindFirstResponder)
- (UIView *)findFirstResponder
{
   if (self.isFirstResponder) {        
        return self;     
   }

   for (UIView *subView in self.subviews) {
       UIView *firstResponder = [subView findFirstResponder];

       if (firstResponder != nil) {
           return firstResponder;
       }
   }

   return nil;
}
@end

I tried this, and sometimes it returned with nil as expected. self.view still got the message sent to first responder.

OTHER TIPS

You can find the first responder with this:

@implementation UIView (FindFirstResponder)
- (UIView *)findFirstResponder
{
    if (self.isFirstResponder) {        
        return self;     
    }

    for (UIView *subView in self.subviews) {
        UIView *firstResponder = [subView findFirstResponder];

        if (firstResponder != nil) {
            return firstResponder;
        }
    }

    return nil;
}
@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top