Re: NSWindow's firstResponder and a pointer to that object.
Re: NSWindow's firstResponder and a pointer to that object.
- Subject: Re: NSWindow's firstResponder and a pointer to that object.
- From: Dustin Voss <email@hidden>
- Date: Fri, 9 Jan 2004 20:36:06 -0800
On 9 Jan, 2004, at 4:44 PM, Johan Kool wrote:
On Jan 9, 2004, at 03:43, Dustin Voss wrote:
On 8 Jan, 2004, at 9:10 AM, Johan Kool wrote:
Hello,
I can't find this anywhere. I am implementing an inspector and are
listening for NSWindowDidBecomeMainNotification notifications. This
gets me the current window, so now I would be interested in which
object is the firstResponder. With [myWindow firstResponder] I get
back a NSResponder, but with that pointer I can't send function
calls to the firstResponder. I tried to send notifications from my
view when it loses the firstResponder state, but as every window has
a firstResponder, this only works when I have multiple views or when
I create a new window.
So my question is: How do I get a pointer to myView in myWindow
knowing that myView is the firstResponder and having a pointer to
myWindow?
Related to this: How do I test that a pointer is to an object of a
certain class?
This shouldn't be a problem. [myWindow firstResponder] returns a
pointer to an NSResponder. This does not mean that the object is an
NSResponder, and only an NSResponder. It just means that the object
can be treated as an NSResponder. The object will actually be an
NSView (or your sub-class of NSView). Specifically, it will be
myView. NSView inherits from NSResponder, so of course you can call
NSResponder selectors, but you can also call NSView selectors.
If you want to check to see if the object is of a certain class, use
the following code:
[object isKindOfClass:[ACertainClass class]]
This will return true if object is an instance of ACertainClass or of
a sub-class of ACertainClass. If you want to exclude sub-classes for
some reason, use isMemberOfClass: instead of isKindOfClass:.
Thanks for your clarification! I should have known this. Stupid me! :-)
I was looking to see if a window/view posts notifications about
changes in the firstResponder. Am I right there is no such thing
unless I implement it into my subclasses myself?
Correct. You subclass NSWindow and override makeFirstResponder:. I did
the following to make a ResponderNotificationWindow class.
- (BOOL) makeFirstResponder:(NSResponder *)aResponder
{
BOOL didMakeFirstResponder = [super makeFirstResponder:aResponder];
if (didMakeFirstResponder)
{
NSNotification *notif = [NSNotification
notificationWithName:@"WindowDidMakeFirstResponder" object:self
userInfo:[NSDictionary dictionaryWithObject:aResponder
forKey:@"FirstResponder"]];
id delegate = [self delegate];
if (delegate != nil && [delegate respondsToSelector:
@selector(windowDidMakeFirstResponder:)])
[delegate performSelector:@selector(windowDidMakeFirstResponder:)
withObject:notif afterDelay:0];
[[NSNotificationQueue defaultQueue] enqueueNotification:notif
postingStyle:NSPostASAP];
}
return didMakeFirstResponder;
}
The delay is there so that whatever call makeFirstResponder can finish
its processing. Until it does, the first responder hasn't finished
changing.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.