Re: Notification problem
Re: Notification problem
- Subject: Re: Notification problem
- From: Bill Bumgarner <email@hidden>
- Date: Wed, 1 May 2002 09:17:13 -0400
As you have already discovered, the notification is named
NSWindowDidBecomeKeyNotification-- you missed the "NS".
However, the paragraph before raised a design question.
Specifically, why does your application need to dispatch commands to
particular subclasses of NSWindowController as derived from the key window
by hand?
The normal implementation pattern of Cocoa is to have each menu item
connected to the First Responder. That is, when the menu item is
selected, the action is sent to the first object in the responder chain
that can respond to that action (the first object in the chain that
implements the action method).
If a window is in the responder chain (the key window always is), then the
window's delegate will also be in the responder chain. As such, if you
implement the action method in your window's delegate, it will be executed
when the user selects the appropriate menu item.
There is no need for manual dispatch of the action and, conveniently, the
responder chain will also automatically enable/disable menu items based on
whether or not anything in the current responder chain responds to the
action of each menu item.
At first, the concept of the responder chain and the 'First Responder'
object in the NIB file may seem confusing. And, in truth, it generally is
confusing to folks that are new to Cocoa because it is hard to believe
that it really is as simple as it is while still being as powerful as it
is.
Let me describe an implementation scenario to see if it helps.
Say you are implementing an application called 'SimDeli: the ultimate Deli
simulation'. In this application, you have three windows; The Cheese
Bar Window, The Soup Bar Window and The Sandwich Bar Window. You want to
add a menu item that tells your simulated customer to 'Smell the
Cheese'. Of course, the Soup Bar window doesn't have cheese while the
other two windows do and, as such, you want the menu item to be disabled
whenever the user has made the Soup Bar Window key. You also want the
user to be able to close the various windows and, for efficiencies sake,
you want the windows to be in separate NIB files.
Assume that you have already implemented the classes CheeseBar, SoupBar,
and SandwichBar. Each implements a -showWindow: method that takes care
of loading the NIB and displaying the window. In their respective NIB
files, these classes are the file's owner of their associated NIB. That
is, in the CheeseBar NIB, the File's Owner is marked as an instance of
class CheeseBar. The three windows for the three bars are in different
NIB files from MainMenu.nib.
To implement the above, you would:
1) Implement the...
- (IBAction) smellTheCheese: sender;
... method on the CheeseBar and SandwichBar classes. Note that
IBAction is simply a macro that replaces the word IBAction with the
keyword void at compile time. I.e. it does nothing special at compile
time. When InterfaceBuilder is parsing a header file, it uses the
IBAction keyword as a marker for methods that should be parsed as an
action method.
2) open MainMenu.nib and add a "Smell the Cheese" menu item to the
appropriate menu
3) In MainMenu.nib, double-click the "First Responder" object.
4) In the inspector window, click on the Actions tab and click the "add"
button. Name the action "smellTheCheese:".
5) Connect the "Smell the Cheese" menu item to "First Responder" and
select the "smellTheCheese:" action.
Run the app because you are done!
When the app is running, bring up the three windows. Notice that the
'Smell The Cheese' menu item is only enabled when the "Cheese Bar" or
"Sandwich Bar" window is key. It is disabled when "Soup Bar" is key.
When enabled-- when one Cheese Bar or Sandwich Bar is key-- notice that
selecting the menu item causes the -smellTheCheese: method on the
appropriate class to be invoked.
b.bum
On Wednesday, May 1, 2002, at 01:45 AM, email@hidden
wrote:
I have a controller object : AppController:NSObject. I want this object
to be told anytime a window owned by the application is made key because
AppController will keep an instance variable and send menu choices to the
correct subclass of NSWindowController (as derived from the current key
window object).
I implemented this in AppController.m:
-(void)init{
//some init stuff here and super init etc.
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(setKeyWindowFromNotification:)
name:@"WindowDidBecomeKeyNotification"
object:nil];
return self;
}
_______________________________________________
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.