Re: Making an object accessible by both MyDocument AND a custom class
Re: Making an object accessible by both MyDocument AND a custom class
- Subject: Re: Making an object accessible by both MyDocument AND a custom class
- From: James DiPalma <email@hidden>
- Date: Wed, 25 Sep 2002 14:45:47 -0400
I wanted to use your post as an example of what is wrong with
cocoa-dev, but it felt wrong to single you out even though you typify a
developer that knows enough (NSApplication does use the responder chain
to find its own delegate when sending messages to a nil target), but
also makes a suggestion that is simply wrong (sending a message to nil
does not send the message up the responder chain).
I chose to only mention my disappointment as a public side comment. And
you responded with another example of what I find so distasteful about
this list. An emphasized post stating that I was wrong, that you were
right, that you have extensive experience being right, and that I am
unfamiliar with Cocoa.
After 14 years of experience, I still consider myself a little
unfamiliar with Cocoa, so you were kind of right, but I have heard
about a nil target before and showed this knowledge by suggesting use
of -sendAction:to:from:
You had 2 chances to verify that sending messages to nil does
something, and you didn't use either one. Before writing this response,
I verified that [nil tellMyCustomObjectToDoSomething]; doesn't do
anything.
Here is what I did:
1. New document based cocoa app in ProjectBuilder
2. New Objective-C class called ApplicationDelegate
3. New Objective-C class called Controller
4. Add code for these classes (see below)
5. Set up instance of ApplicationDelegate as applications delegate in
MainMenu.nib (as you describes)
6. Set up instance of Controller in MyDocument.nib
7. Add button to MyDocument.nib's window. Its target is Controller its
action is -testNil:
Here is my code:
// In ApplicationDelegate.m
- (void) tellMyCustomObjectToDoSomething
{
NSLog(@" Did Tell");
}
- (void) tellMyCustomObjectToDoSomething:(id)sender
{
NSLog(@" Did Tell");
}
// In Controller.m
- (void) testNil:(id)sender
{
NSLog(@"test nil message");
[nil tellMyCustomObjectToDoSomething];
NSLog(@"test nil target");
[[NSApplication sharedApplication]
sendAction:@selector(tellMyCustomObjectToDoSomething) to:nil from:self];
NSLog(@"test nil action message");
[nil tellMyCustomObjectToDoSomething:self];
NSLog(@"test nil target action");
[[NSApplication sharedApplication]
sendAction:@selector(tellMyCustomObjectToDoSomething:) to:nil
from:self];
}
Here is what this program outputs:
2002-09-25 14:05:14.953 Nil[2641] test nil message
2002-09-25 14:05:14.953 Nil[2641] test nil target
2002-09-25 14:05:14.954 Nil[2641] Did Tell
2002-09-25 14:05:14.954 Nil[2641] test nil action message
2002-09-25 14:05:14.954 Nil[2641] test nil target action
2002-09-25 14:05:14.954 Nil[2641] Did Tell
-jim
From: Joe Lester <email@hidden>
You certainly CAN send messages to nil! I do it all the time. It sends
the message up the responder chain. Have you not heard of a nil target
before? Can I get a witness out there please?
On Wednesday, September 25, 2002 11:59 AM, James DiPalma
<email@hidden> wrote:
You could make sure that MyCustomObject was the delegate of NSApp.
The
easiest way to accomplish this would be to connect them in
MainMenu.nib... File's Owner -> MyCustomObject. Then you could
message
MyCustomObject from most any object by sending messages to nil. The
messages would go up the responder chain and end up at your single
instance of MyCustomObject.
[nil tellMyCustomObjectToDoSomething];
NO! sending a message to nil will do nothing.
Look at NSApplication and documentation for its method
-sendAction:to:from: it gives a brief description of how the responder
chain is used when sending actions.
-jim
P.S. I respect that people are trying to do what is right by
participating and answering questions, but this post contains enough
information to sound like it might be correct, but is in fact
very
misleading.
_______________________________________________
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.
_______________________________________________
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.