I am very new to cocoa and really confused with the whole concept of the first responder. When does an object become a first responder? Assuming each object in my view hierarchy accepts a first responder status, if no explicit "becomeFirstResponder" is made, what is the default first responder after an app is launched?
UPDATED:
I should probably be a bit more specific about my confusion, which stems from the following experiments I did to study the behaviour of first responder.
Experiment 1):
The goal of this experiment is to detect "mouseMoved" from a first responder. According to the spec, "mouseMoved" is always sent to the first responder first.
I constructed the following hierarchy in Xcode:
NSWindow - NSView - myCustomView
myCustomView inherits from NSView and is implemented as follow:
@implementation myView
- (void)awakeFromNib
{
[[self window] setAcceptsMouseMovedEvents:YES];
}
- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor blueColor] set];
NSRectFill([self bounds]);
}
- (void)mouseMoved:(NSEvent *)theEvent
{
NSLog(@"[MYCUSTOMVIEW] mouse moved");
}
@end
When I ran the above code, nothing is printed out as I move my mouse on the screen. This is expected, as NSWindow is the first responder. myCustomView never received the "mouseMoved".
Experiment 2):
I added the following to myCustomView's implementation:
- (BOOL)acceptsFirstResponder
{
return YES;
}
When I ran the code, still nothing is printed out. Now I am confused, shouldn't myCustomView have become the first responder in this case and receive "mouseMoved" event?
What's interesting is, when I clicked the mouse inside the bounds of myCustomView, then the "[MYCUSTOMVIEW] mouse moved" message started to show up in the console. I can understand the clicking action makes myCustomView the first responder, but why wasn't it in the first place?
If I ran the code again, this time, the "[MYCUSTOMVIEW] mouse moved" message showed up right from the beginning. I didn't have to click myCustomView anymore. What is going on?
Experiment 3):
I changed "acceptsFirstResponder" to return "NO" and ran the code. The "[MYCUSTOMVIEW] mouse moved" message still showed up from the beginning. What the! myCustomView is no longer interested in being the first responder. Why is it still receiving "mouseMoved" event?
This is really confusing to me. I will be really appreciated if someone can tell me what is happening above, and how is the first responder set initially at launch?
Thanks!