NSArrayController: How to programmatically clear selection? - cocoa

Very simple question that's driving me crazy: What's the proper way to clear the selection of an NSArrayController programmatically?
I'm designing a view with the following components:
NSArrayController *controller1: Bound to an array of objects
NSPopUpView view1: Content bound to controller1.arrangedObjects; Value bound to controller1.selection; "Inserts Null Placeholder" selected
NSArrayController *controller2: Bound to an array stored in controller1.selection
NSPopupView view2: Content bound to controller2.arrangedObjects; value bound to controller2.selection; "Inserts Null Placeholder" selected
Initially, view1's content is populated; controller1 and controller2 have nil selection values; and view1 and view2 display null placeholders. Selection of controller1 causes controller1's selection to change and view2's content to populate. All good.
I'd like to implement a Clear button that clears the selection of controller1, which, thanks to bindings, should also clear the selection of controller2 and reset view1 and view2 to the null placeholder. For the life of me, I can't figure out the proper code for this very simple function. Altering the selection of controller1 fails to update the value shown in view1. Worse, altering the controller1 selection programatically causes weird things to happen in controller2: further selection of values in view1 fails to have any effect on view2.
Things I've tried:
Calling the SetSelectedObjects method of controller1 with an [NSArray new].
Calling the SetSelectedObjects method of controller1 with null.
Calling the SetSelectedIndex method of controller1 with NSNotFound.
Calling the RemoveSelectedIndex method of controller1 with the SelectedIndex property of controller1.
Looking in the Cocoa NSArrayController documentation for any class method or suggestion for clearing the selection value. Nothing there - not even any mention of this being desirable, let alone how to accomplish it.
Any ideas? Thanks...

According to Apples Developer documentation this can be done using setSelectionIndexes:
To deselect all indexes, pass an empty index set.
Objective-C:
[arrayController setSelectionIndexes:[NSIndexSet indexSet]];
Swift:
arrayController.setSelectionIndexes( NSIndexSet() )

Try controller1.selectionIndex = NSIntegerMax; and see if that works. I did a simple test with a label bound to the array controller's selection, and when I set the selectionIndex to NSIntegerMax, the no selection placeholder was displayed in the label.

As it turns out, the NSArrayController page does include a single recommendation for clearing the selection:
setSelectionIndexes:
Sets the receiver’s selection indexes and returns a Boolean value that indicates whether the selection changed.
(BOOL) setSelectionIndexes: (NSIndexSet *) indexes
Discussion
Attempting to change the selection may cause a commitEditing message which fails, thus denying the selection change.
To select all the receiver’s objects, indexes should be an index set with indexes [0...count -1]. To deselect all indexes, pass an empty index set.
However, I had actually tried that, and it still left me with a problem.
To generalize:
Create two classes, A and B, where A contains a property "NSArray *b_list" that contains a list of instances of B's.
Create an application with a property "NSArray *a_list". Populate it with some instances of A, and populate the b_list of each A instance with some B instances.
Create a window with two array controllers, Controller_A (bound to a_list) and Controller_B (bound to Controller_A.selection.b_list).
Create two pop-up buttons in the window, Popup_A (bound to Controller_A.arrangedObjects) and Popup_B (bound to Controller_B.arrangedObjects).
Create a "Clear" button with some logic to clear the selection of Controller_A. ("Some logic" is either the method recommended in the Apple documentation, or any other method.)
Run the application. Select an entry in Popup_A. Notice that Popup_B populates with the instances of Controller_A.selection.b_list, as it should.
Now hit the Clear button.
ERROR: Note that while the content and selection of Popup_A correctly become null, the same doesn't happen in Popup_B: it presents a blank entry with a single selected (blank) item. Note also that the selection properties of Controller_B indicates that there is a selection, but its properties are weird: selectedIndex points to 0 instead of NSNotFound, and selectedIndexes includes a non-empty selection integer range.
This is clearly an error with the binding logic that can definitely cause some exceptions and logical errors. For example, if there's any sort of binding attached to B_controller.selection, clearing A_controller will raise an exception relating to the selection value in B_controller, since it indicates a selection but points to garbage.
My workaround is not to bind anything directly to the B_controller selection. Instead, access B_controller programmatically, and in view of the selection value of A_controller, like this:
// setting some property c to the value of b_controller:
if ((B_controller.selectedIndex == NSNotFound) || A_controller.selectedIndex == NSNotFound))
c = nil;
else
c = [B_controller objectAtIndex: [B_controller.selectedIndex]];
I'm also submitting a bug report to Apple with this info.

Answer to a very old question but still.
I just needed the same thing, and for me this worked:
[arrayController setAvoidsEmptySelection:NO];
NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:
NSMakeRange(NSNotFound, 0)];
[arrayController setSelectionIndexes:indexes];

Related

How to update an NSTextField programatically and have value propagate through binding

I have a case where I would like to programmatically assign the first responder to an NSTextField and then programmatically insert a value into the NSTextField as well.
I have this working with the following code:
field.window?.makeFirstResponder(field)
field.stringValue = "Auto generated value"
In the example above, field has a binding to my model. If the user chooses to edit Auto generated value and then hit the return key, the first responder is resigned and the edited value is set on my model. BUT, if the user chooses not to edit Auto generated value and hits the return key, the first responder is resigned but the model is not updated.
It's as if the field is not marked as dirty when programmatically updating the value.
I would like to avoid having to manually update the model when the case above occurs due to some complexities that I have left out in the example. I would like it to apply through whichever binding is set just like it would when the user manually types in the value.
Is this possible?
If you use binding with a NSTextField or any other AppKit control, you should perform programmatically value changes on the data source / model, instead of the other way around by manipulating the control value in code. By doing so, you will not have the problems as described in your question.
Inserting the text in the field editor starts an editing session (undocumented feature).
if window.makeFirstResponder(textField1),
let fieldEditor = textField1.currentEditor() as? NSTextView {
fieldEditor.insertText("Auto generated value", replacementRange: NSRange(location: 0, length: fieldEditor.textStorage?.length ?? 0))
}

Syntax of Model Key Path - Bindings xcode 5.1.1

Hey I am new to mac development and I want to use bindings (xcode 5.1.1).
I want to set the Title of a radio button dynamically by an entry of an array controller. I am looking for something like a syntax description how I can perform it.
e.g. something like value1 WHERE value2="bla"
If I trying to search at google I always find solutions which did it programmatically.
Is there anywhere some examples which show me the syntax I can use in this field?
The picture below should you show what I mean.
Answering the question as clarified in the comments…
First, bindings is not always the right technology. It can simplify some things, but it can't do everything and even for some of the things that it can do, it doesn't necessarily make them simpler.
Radio buttons are often organized in an NSMatrix. In that case, you can bind the matrix bindings to track the selection. There are three content-related bindings for a matrix, which can be kind of confusing. The "content" binding is the base. In some cases, it's sufficient. However, if there's a distinction between the object being bound and the value that should be shown by the cells of the matrix, then you can bind the "contentValues" binding to be a subpath of the content binding. That is, it needs to be the same as the content binding with possibly additional elements added to the end of the model key path.
Furthermore, if you want the selected object to be distinct from the content object, you can bind "contentObjects" to a subpath of the content binding.
For example, there may be an array controller whose content is a bunch of Person objects. The matrix content binding might be bound to that array controller's arrangedObjects. If you leave it like that, the cells of the matrix will be populated from the description of each Person object. However, you could bind the matrix's contentValues to the array controller, arrangedObjects, model key path fullName. Then, the matrix cells will be populated with the full name of each Person object.
If you then bind the matrix's selectedObject binding to a property on your window controller, that property will be set to the selected Person object each time the matrix selection changes. If you would prefer, you could bind the matrix's contentObjects binding to the array controller, arrangedObjects, model key path uniqueID. In that case, the window controller property would not be set to the selected Person object itself, but to its uniqueID property.
Alternatively, you could bind the matrix's selectedIndex binding to a controller property. If you use the window controller, then that just directly sets a property on the window controller to indicate the index of the matrix's selection. Or you could bind it to the array controller's selectedIndex property, in which case the selection is "stored" in the array controller.
You need a keypath that takes no parameter as described in the key-value coding (KVC) reference.
By binding to an array controller's selection, if the selection collection is one object with a property or method "value1," then the binding runtime is calling the method valueForKeyPath:#"value1".
The NSObject protocol has performSelector:withObject, but there is nothing like valueForKeyPath:withObject in the KVC protocol or the NSKeyValueBindingCreation protocol
That said, registering dependent keypaths can provide some equivalent behavior...
+ (NSSet*) keyPathsForValuesAffectingValue1
{
return [NSSet setWithObjects:#"value2",nil];
}
... and that would ensure that any time value2 changes, the binding to value1 is re-evaluated.

Setting a default selection for an NSPopupButton?

Is it possible to set a default selection on an NSPopupButton? I have one that allows the user to select the type of server they want to set up, but since an NSPopupButton always shows the first item, they may ignore it if that's the type they want. However, even though that item is being displayed, calling -selectedItem returns (null). Everything works fine if the user picks an item from the menu first.
The Button's content and contentValues are bound to the same Array Controller, which in turn is bound to the keys property of an NSDictionary. I've tried binding the selectedIndex to a variable in the controller and updating that in code, but it has no effect. (I may just be binding it wrong...) How can I select the first item by default?
Thanks in advance!
SphereCat1
When using Bindings, you don't need to and shouldn't get any model info—neither the model itself nor selection state—from the views directly. Talk to the controller that owns the model and the selected indexes.
Note that “index” doesn't have any meaning for an NSDictionary, and keys is not a property of an NSDictionary. (Indeed, I would not be surprised if you were to get an exception because your dictionary does not have an object for the key “keys” in it.) It is a method, and not the accessor kind, so while you can ask the dictionary for the value of that method using Key-Value Coding, you should not.
What you should do is make model objects representing the server types, and hold an array of those, and bind the array controller's content to the property whose value is that array. Bind the pop-up button's contentValues to a name property of your model objects, which should hold the localized name of each server type.

Table view not updating according to bindings - Part Two

Title borrowed from this question, of which this one is not a duplicate. See my answer there for what was wrong for that questioner; I'm the author of that answer, and my problem is not that one.
I have a table view with three columns, whose Value bindings are bound to three properties of the arrangedObjects of an array controller. The array controller's contentArray is bound to the visitationResults of my document object; the items in that array are instances of a model class (VisitationResult). I have also bound the array controller's selectionIndexes and sortDescriptors to properties of my document.
I am mutating my property through a couple of accessors:
- (void) addVisitationResult:(VisitationResult *)newVisitationResult {
[self insertObject:newVisitationResult inVisitationResultsAtIndex:[self countOfVisitationResults]];
NSLog(#"arrayController arrangedObjects: %#", [arrayController arrangedObjects]);
}
That NSLog statement runs, and confirms that the array controller is gathering and arranging my model objects. This means that I am going through my property and getting KVO notifications for my document (which proves that the earlier questioner's problem, that of bypassing the property, is not the problem I'm having).
I added NSLog statements in my model object class's accessor methods. One of them is being called—by the array controller, in order to sort the objects (that property is the sort key). The other two, which the array controller doesn't know about, never get called.
Thus, my table view remains blank.
I found the problem: It's because I had explicitly bound the selectionIndexes and sortDescriptors bindings of the table view.
This wasn't necessary, anyway: I just checked, and the documentation says:
selectionIndexes
Typically, selectionIndexes is bound automatically to the NSArrayController that the first NSTableColumn is bound to.
sortDescriptors
Typically this binding is created automatically, binding to the sort descriptors of the NSArrayController of the initially bound NSTableColumn.
It appears that not only is it not necessary, but binding either or both of these two will break the table view.

Enabling NSButton with bindings, based on NSTableView selection

I have a NSWindow containing an NSButton and an NSTableView.
I'd like the button to be enabled if and only if the table contains at least one item, and exactly one item is selected. (The table does not allow multiple selection.)
What can I bind the button's enabled binding to to make this happen?
This is an old thread, but here are my 2 cents:
Use and array controller and bind the button's enabled state to
Controller Key: selectedObjects
Model Key Path: #count
Works fine.
Try binding to the array controller's selectedObjects, model key path count, with no value transformer.
Note that this would be unsafe if you allowed multiple selection: For one thing, the count could easily be neither YES nor NO; for another, if the user selected a multiple of 256 items, the lowest byte of the count would be 0, so the BOOL value would be NO even though there is a selection.
I ran into this today and I got it to work after some efforts.
My button should be disabled if nothing is selected in the "Master Table":
Problems I ran into:
Use the actual button and not the enclosed Button cell
Specify NO = disabled for Multiple values, No selection etc.
Bind the Enabled property to the Master Table's selection and use a property (code in my case), which is present.
Use the transformer NSIsNotNil to enable the button if something is selected in the master table.

Resources