PDA

View Full Version : [Resolved] Binding Radio Buttons?




ArtOfWarfare
Apr 29, 2012, 09:59 PM
Hi... first time I've asked a Cocoa programming question here (in the past I've always been in the iOS section asking about Cocoa Touch,) but... hope you guys are just as helpful.

Here's what I have:
- Two radio buttons
- A property for which one is selected.
- A property for whether they're enabled or not.

The two properties are bound to the buttons. The enabled binding works perfectly. The selection one, on the other hand, only seems to work in one direction. When the user clicks on a button, the app responds properly. When I attempt to set the property programmatically though, the button doesn't set itself graphically.

Any idea what I'm doing wrong? Here's some of the code...

From the header:
@property (setter = setPreferred:) int preferred;
@property BOOL allowPrefer;

setPreferred: just stores the setting in NSUserDefaults (along with setting it, of course.) Both preferred and allowPrefer are synthesized.

if (allowExternal && (!allowInternal))
{
// Force preferred to be 0 (external only.)
self.preferred = 0;
self.allowPrefer = NO;
}

else if (allowInternal && (!allowExternal))
{
// Force preferred to be 1 (internal only.)
self.preferred = 1;
self.allowPrefer = NO;
}

else
{
self.allowPrefer = YES;
}

I think that covers everything... any idea what I'm doing wrong?



chown33
Apr 30, 2012, 12:14 AM
IIRC, Cocoa bindings uses KVC (Key Value Coding). Setting a property using the dot notation does not automatically lead into KVC (or KVO). To get a KVC action, you should use setValue:forKey:, or setValue:forKey:. In short, KVC uses properties (by default), but properties do not inherently use KVC.

See this, under the heading "Dot Syntax and Key-Value Coding"
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/BasicPrinciples.html%23//apple_ref/doc/uid/20002170-BAJEAIEE

Also see this:
http://www.tomdalling.com/blog/cocoa/how-cocoa-bindings-work-via-kvc-and-kvo

And this:
http://www.macresearch.org/cocoa_for_scientists_part_xi_the_value_in_keys

Google search terms: kvc cocoa

It's been a long time since I even looked at any KVC or KVO, and even when I did, I didn't get far into it before we decided to go another route. So my understanding may well be flawed or incomplete.


And please fix the title: "ratio" -> "radio".

ArtOfWarfare
Apr 30, 2012, 07:34 AM
:-/

I made the suggested change, (I replaced the lines "self.preferred = 0;" and "self.preferred = 1;" with "[self setValue:[NSNumber numberWithInt:0] forKey:@"preferred"];" and "[self setValue:[NSNumber numberWithInt:1] forKey:@"preferred"];", respectively,) but it didn't change the behavior of my app.

I'm thinking I may just add in some "noodle code" that'll force the button to highlight properly.

Edit:

Actually, I can't figure out how to get it to programmatically highlight at all... it says neither the selectedRow nor selectedTag property of an NSMatrix has a setter, and the other properties don't seem to take integers as arguments?

Edit 2:

Solved!

As I showed in my opening post, I'm using a custom setter method for my variable preferred. After doing some NSLogging, I found it odd that the instance variable never seemed to change. And then I thought of taking a look at something.

- (void)setPreferred:(int)setPreferred
{
[[NSUserDefaults standardUserDefaults] setInteger:setPreferred forKey:@"preferred"];
[data setPreferInternal:(BOOL)setPreferred];
[data collectLocalData];
}

What a lovely setter method... it passes on the data to some other instances of helper classes and the like... saves the setting so it'll be there the next time it launches... good, good, right? Oh wait! It doesn't set!

So, here's the good, working code... my apologies for wasting time... doesn't really have too much to do with bindings, or NSMatrix or Radio buttons... just simple properties and custom setter methods.

- (void)setPreferred:(int)setPreferred
{
preferred = setPreferred;
[[NSUserDefaults standardUserDefaults] setInteger:setPreferred forKey:@"preferred"];
[data setPreferInternal:(BOOL)setPreferred];
[data collectLocalData];
}