NSTableView - Responder - Focus - The Solution
NSTableView - Responder - Focus - The Solution
- Subject: NSTableView - Responder - Focus - The Solution
- From: styx <email@hidden>
- Date: Fri, 27 Feb 2004 23:08:33 -0500
Hi,
I had a problem with two NSTableView where I wanted to add an entry
with one button.
It seams that this is not as easy as one would think, at least not for
me as a newbie, anyway.
My goal was to have one button which is disabled when neither of the
tables are selected.
And which adds an entry to either of the tables depending on which one
is selected.
With great help from Cricket I could solve the problem and thought I
post it here.
Please let me know if this is inappropriate or if there is any
objection.
If you have a question just ask.
Styx
Here is the code.
------------------------------------------------------------------------
---------------------
In MyDocument.h
----------------------------------------------------------------------
// Selector for enable addButton
- (void)enableAddButton:(NSNotification *)notification
{
[addButton setEnabled: YES];
}
// Selector for disable addButton
- (void)disableAddButton:(NSNotification *)notification
{
[addButton setEnabled: NO];
}
- (id)init
{
// Install observers
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(enableAddButton:)
name:@"EnableAddButton" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(disableAddButton:)
name:@"DisableAddButton" object:nil];
}
// Check which of the tables are active and send the appropriate message
- (void)addEntry:(id)sender
{
NSTableView *tv = (NSTableView *) [[tableLeft window] firstResponder];
if (tv == tableLeft) {
[self addToLeftTable];
} else if (tv == tableRight) {
[self addToRightTable];
}
}
- (void)addToLeftTable
{
// Add an entry to the left table
}
- (void)addToRightTable
{
// Add an entry to the right table
}
----------------------------------------------------------------------
In MyTableView.h which is a subclass of the left and right NSTableView
----------------------------------------------------------------------
// Send notification to MyDocument to enable the addButton
- (BOOL)becomeFirstResponder
{
[[NSNotificationCenter defaultCenter]
postNotificationName:@"EnableAddButton" object:self];
return [super becomeFirstResponder];
}
// Send notification to MyDocument to disable the addButton
- (BOOL)resignFirstResponder
{
[[NSNotificationCenter defaultCenter]
postNotificationName:@"DisableAddButton" object:self];
return [super resignFirstResponder];
}
_______________________________________________
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.