Re: Binding NSButton enabled state
Re: Binding NSButton enabled state
- Subject: Re: Binding NSButton enabled state
- From: "Lorenzo Thurman" <email@hidden>
- Date: Sun, 20 Apr 2008 12:47:17 -0500
On Sat, Apr 19, 2008 at 11:55 PM, Chris Hanson <email@hidden> wrote:
> On Apr 19, 2008, at 11:08 AM, Lorenzo Thurman wrote:
>
> I have two NSTableViews, tableA and tableB, each managed by separate
> > NSArrayControllers. When a selection is made in either table, an
> > instance
> > variable is set. I want to have an NSButton's enabled state to 'YES'
> > whenever the instance variable is set.
> >
>
> Don't think of it as an instance variable, think of it as a property.
> Restating your problem, you want your "Do Something" button's enabled
> property to be bound to your window controller's "can do something"
> property.
>
> What this means is that you need to manipulate your window controller's
> "can do something" property in a way that things bound to it can notice --
> in other words, in a way that will post key-value observing notifications.
> Thus instead of manipulating it as an instance variable, you should just
> always invoke its setter.
>
> You haven't said how you're actually noticing that the selection in one of
> your tables has changed; I assume you're either using an NSTableView
> delegate method or a notification to do so.
>
> -- Chris
>
>
Here's my code:I register to receive these notifications when the selection
changes.
-(void)applicationDidFinishLaunching:(NSNotification*)not{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(
usTableViewSelectionDidChange:) name:
NSTableViewSelectionDidChangeNotification object:usCityTable];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(
intlableViewSelectionDidChange:) name:
NSTableViewSelectionDidChangeNotification object:intlTable];
}
These are the messages sent when a selection is changed (Is there a way to
use just one?):
-(void) usTableViewSelectionDidChange:(NSNotification*)not{
NSArray* selObjs = [usTableContent selectedObjects];
if([selObjs count] > 0) {
[intlTable deselectAll:self];
NSDictionary* selectedDictionary = [selObjs objectAtIndex:0];
[self setValue:[selectedDictionary objectForKey:@"location"] forKey:
@"location"];
[self setSelectedItem:selectedDictionary];
}
}
-(void) intlableViewSelectionDidChange:(NSNotification*)not{
NSArray* selObjs = [intlTableContent selectedObjects];
if([selObjs count] > 0){
[cityTable deselectAll:self];
NSDictionary* selectedDictionary = [selObjs objectAtIndex:0];
[self setValue:[selectedDictionary objectForKey:@"location"] forKey:
@"location"];
[self setSelectedItem:selectedDictionary];
}
}
The "location" instance variable is what should trigger a change in the
button state. There are also two labels in the window that are bound to two
other items in "selectedDictionary", city & state. These labels change as
expected when clicking between tableviews. My "Save" button is misbehaving.
When I select an item in the usCityTable, the button enables, but will
disable when I select from the intlTable. The "Save" button is bound to
mycontroller.location. The two labels are bound to
mycontroller.selectedItem.city and mycontroller.selectedItem.state. I've
tried binding the button to mycontroller.selectedItem.location, but the
behavior is the same and using arrangeObjects.@count does not provide the
desired behavior as the button is enabled even when no selection is made.
This, I think, is all the relevant code and bindings. I've reduced this a
small project and code post the entire project, if you want to take a look.
Thanks
-
--
"My break-dancing days are over, but there's always the funky chicken"
--The Full Monty
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden