NSUserDefaults for NSMatrix - cocoa

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.

I guess you can try observer patttern instead of action pattern.
Cocoa - Notification on NSUserDefaults value change?

Related

multiple (redundant?) observeValueForKeyPath: messages received

I have an obj-C Mac app with an object observing some property (bool) of standardUserDefaults.
The code is pretty standard:
[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:#"property" options:NSKeyValueObservingOptionNew context:nil];
This standardUserDefaults' property is bound to an NSButton state.
When I click the button, the property changes as normal, but up to 8 messages in a row notifying a change in the property are received at each click! I have no idea why that is. The property is not bound to anything else but the button. I checked that the button is indeed clicked once. The value of the property is the same across notifications resulting from the same click.
This also occurs without binding if I manually change the property via
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:#"property"];
After that, 8 observeValueForKeyPath: messages are received in a row!
The only related issue I found was this one: https://developer.apple.com/forums/thread/47609.
Any idea?

recalculateKeyViewLoop doesn't work

In my cocoa application I want to change the key order of my views. I fill in the nextKeyView reference for all subviews in the desired order. In the view's awakeFromNib function I do the following:
[[self.view window] setInitialFirstResponder:self.view];
[[self.view window] setAutorecalculatesKeyViewLoop:NO];
[[[self view] window] recalculateKeyViewLoop];
where the nextKeyView for the self.view is set to the first subview I want to appear in key order.
Nothing helps and the the key ordering stays default. How to resolve it?
Thanks
Calling recalculateKeyViewLoop forces the window to automatically calculate the key-view loop. You don't want to do this if you've set the key-view loop manually, as it does exactly what you've just told the window not to do in [[self.view window] setAutorecalculatesKeyViewLoop:NO].
You don't need any of this code. In Interface Builder, make sure your window has the "Auto Recalculates View Loop" checkbox un-checked, and connect the initialFirstResponder outlet of your window to the view that you want to be the initial first responder.
No code required.
following on from Rob's answer, in XCode 7.3 you can find the checkbox in the Interface Builder for the main window under:

how do i stop NSTextField blocking right clicks for NSTableView rows?

As the title suggest, I've enabled right clicks for my tableview with customized rows. Anywhere there is an NSTextField, it blocks the right click.
is there a userInteractionEnabled equivalent for cocoa like on the iphone?
I though I probably needed to subclass everything in my NSTableCellView subclass, but I just needed to override -(NSView*)hitTest:(NSPoint)aPoint method in my NSTableCellView subclass to return self.
I'm able to right click on text fields within my custom view based table view cell. Here is how I configure it:
NSTextField *tf = [[NSTextField alloc] initWithFrame:NSZeroRect];
self.textField = tf;
self.imageView.autoresizingMask=NSViewWidthSizable;
self.textField.editable=NO;
self.textField.selectable=NO;
self.textField.drawsBackground=NO;
self.textField.bordered=NO;
self.textField.bezeled=NO;
self.textField.target=self;
self.textField.action=#selector(textDidEndEditing:);
[self.textField.cell setLineBreakMode:NSLineBreakByTruncatingMiddle];
Also, make sure you are setting the -menu property of NSTableView and not the cell view to enable to menu. (I don't know if that will make a difference to your issue but it is how I do right clicking in a table view.)

NSTableView selectable but not editable

Trying to get an NSTableView in IB to be selectable but not editable. But de-selecting "Editable" for a column also removes the selecting capability.
Can someone tell me where I should insert this code to make it work (not working in app delegate or window controller) :
NSTextFieldCell *aCell = [tableColumn dataCell];
[aCell setEditable: NO];
[aCell setSelectable: YES];
BTW that table is updated by dictionary bindings, and the dictionary controller is set to not editable.
Set the columns to Editable, but the individual cell behaviour to Selectable.
I'd try implementing tableView:shouldEditTableColumn:row:in your NSTableViews delegate and return NO. See here.

Sorting a NSArrayController backed NSTableView

I have a NSArrayController and a NSTableView. They show tracks from iTunes. I can sort the list by clicking in the header.
Is there a way to set up a default sort descriptor for the table view so it sorts for albums every time the user launches the app?
I tried to set the sortDescriptor on the array controller and the table view but that changes nothing.
Thank you
Edit: The answer is right. But it needs a NSArray:
- (NSArray *)mainSortDescriptor {
return [NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:#"album" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:#"trackNumber" ascending:YES],
nil];
}
If you want to bind the array controller's sort descriptor, you have to bind it to something. You can put this in your application delegate, for example:
- (NSArray *)tracksSortDescriptors {
return [NSArray arrayWithObject:
[NSSortDescriptor sortDescriptorWithKey:#"albumName"
ascending:YES]];
}
Then you can set up the binding in IB as
Bind to: MyAppDelegate
Model Key Path: tracksSortDescriptors
EDITED. I forgot, when translating this from PyObjC, that I was returning a list. Oops.
I tried this, didn't quite work - resorted on each app start, but not while the app was running.
Eventually, I noticed that in my NSArrayController object, the following box was unticked (argh!):
"Auto rearrange content"
...so, FYI to anyone who has the same problem: make sure that box is ON :)

Resources