ila
ila

Reputation: 920

What is my FirstResponder

I created an application and in ViewController.m , viewDidLoad , i wrote following code

 for (UIView* v in self.view.subviews){
        NSLog(@"View is %@",v);
        NSLog(@"First Responder is %@",[v isFirstResponder]?@"YES":@"NO");
    }
NSLog(@"First Responder is %@",[self isFirstResponder]?@"YES":@"NO");
NSLog(@"First Responder is %@",[self.view isFirstResponder]?@"YES":@"NO");

But it it returns NO for everything. What is my first responder by default ?

Upvotes: 0

Views: 606

Answers (2)

WDUK
WDUK

Reputation: 19030

From documentation for OS X:

Determining First-Responder Status

Usually an NSResponder object can always determine if it's currently the first responder by asking its window (or itself, if it's an NSWindow object) for the first responder and then comparing itself to that object. You ask an NSWindow object for the first responder by sending it a firstResponder message. For an NSView object, this comparison would look like the following bit of code:

if ([[self window] firstResponder] == self) {
    // do something based upon first-responder status
}

Note: This is for OS X. Unfortunately iOS doesn't have a similar system, but can be achieved via:

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

This is undocumented, and could be rejected by Apple. For testing and exploratory research, this is handy for you.

Here are some documents on first responders in iOS:

Upvotes: 1

jimpic
jimpic

Reputation: 5520

In your main view controller, do this:

for (UIView *sub in self.view.subviews)
{
    if (sub.isFirstResponder)
        NSLog(@"It's me!");
}

Upvotes: 1

Related Questions