Archive for September 19th, 2009


Tip: First Responder

So you ever found yourself thinking “gee, it would be really handy to know what the first responder is right now” and wonder why you couldn’t find what must be the quite obvious call to get it? Well, we did, and it turns out no, there actually is no supported way to do it far as we can tell. But if you really really need to figure it out, Matt Gallagher is on the job:

This is an important piece of information, so it’s strange that Apple didn’t choose to provide a public method to access it. Curiously, there is a method, firstResponder, on UIWindow which returns this value but it isn’t public. This will work:

UIView *firstResponder = [[UIApplication sharedApplication] keyWindow] performSelector:@selector(firstResponder)];

Now, you do have the UIResponder isFirstResponder method publicly available if you want to query about a specific view you know about, but the generic case, nope. Most likely because arbitrary access would get you closer to the dreaded “There is no documentation for the custom subclasses or self-contained views of…” rejection notice, I suppose. But hey, if you want to know how, there you go.
Also of note in that article is code for finding the keyboard, drawing round rects, and providing a nice-looking progress view; good stuff all!