0

Thanks for the help. I have an NSMatrix with 2 radioButtons set to retain the user defined selection using the selectedIndex binding. I also have 2 button actions that toggle the same NSMatrix radioButton selection(s).

[sizeMatrix selectCell:[sizeMatrix cellWithTag:0]];

and ...

[sizeMatrix selectCell:[sizeMatrix cellWithTag:1]];

In these respective (button) actions, I need to set the necessary NSUserDefaults for the linked NSMatrix selection. I tried the following with no success:

[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:0] forKey:@"myKey"];

How do I set this up? As well, how do I set the userDefault in awakeFromNib;?

Thanks again.

-paul.

Paul
  • 189
  • 10
  • As you are talking about *binding* why don't you simply ***bind*** `selectedIndex` to `Shared User Defaults Controller` with appropriate key in Interface Builder? – vadian Jun 12 '18 at 17:18
  • Thanks, The NSMatrix object is implemented as you describe and works as expected. I have two separate NSButton actions that toggle the selection of the noted NSMatrix radioButtons. As it stands the code in these actions doesn't set and/or retain the userDefault setting. I need to do this programmatically. – Paul Jun 12 '18 at 17:48
  • 2
    When you bind the `selectedIndex` of the matrix due to KVO the value is saved to UserDefaults automatically when it changed. That's the main benefit of Cocoa Bindings. – vadian Jun 12 '18 at 18:01
  • Well it's not working as you describe. The matrix setting is retained when I directly toggle it's radioButtons. That's not the issue. As noted I have two entirely independent push button actions (as described above) that programaticlly flip the state of the (2) matrix radioButtons. When I use either of the button actions, the matrix radio buttons state work as expected. However the radio button state is not loaded in the userDefaults. Sorry for the confusion. – Paul Jun 12 '18 at 18:19
  • Did you check the user defaults? Is the same `myKey` written by the matrix and in code? – Willeke Jun 15 '18 at 12:14

1 Answers1

0

I guess you can try observer patttern instead of action pattern.

Cocoa - Notification on NSUserDefaults value change?

MyCometG3
  • 167
  • 1
  • 10