Re: What do NSArrayController, etc. do for me?

2017-01-13 Thread Quincey Morris
On Jan 13, 2017, at 09:52 , Charles Srstka  wrote:
> 
> What does NSObjectController do? Well, not a lot, but what it does is pretty 
> helpful:

There’s one other obscure thing that NSObjectController does (or did, since I 
don’t know if this situation still exists, but I doubt the bug will ever be 
fixed). When you bind a text field directly to an undoable model property, the 
undo chain gets messed up after certain combinations of undo and redo. There 
are actually two undo chains involved, because the window maintains an internal 
undo chain for typing which is grafted onto the top of your app’s undo chain 
while editing is in progress. For some reason, binding through a 
NSObjectController eliminates the problem in most cases.

There are a few other things that NSArrayController can usefully do that you 
didn’t mention explicitly:

— As well as shadowing the table view’s selection, it provides markers that 
represent “multiple selection” and “no selection” that Cocoa bindings are aware 
of.

— It supports the more intricate set of bindings that (say) NSPopupButton uses 
for its menu items’ checked and enabled states, and item text. Some of these 
things, if used, are required to be bound via array controllers.

— It has a standardized mechanism for handling validation errors, including the 
display of an error dialog.

In general, NS…Controller classes are glue classes that support the behavior of 
Cocoa bindings. That means, in general, when you use bindings, it’s desirable 
to bind via a NS…Controller for consistent UI behavior.

(OTOH, NS…Controller classes are horrible, inscrutable black boxes. But that’s 
a different rant.)

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: What do NSArrayController, etc. do for me?

2017-01-13 Thread Charles Srstka
> On Jan 13, 2017, at 6:39 AM, Daryle Walker  > wrote:
> 
> If my data model supports KVC/KVO, it seems like I should be able to bind my 
> views to my data directly via Cocoa Bindings, without using NSArrayController 
> or NSObjectController or the like. What advantage do those classes give me?

I just realized you were asking about NSObjectController as well, not just 
NSArrayController. What does NSObjectController do? Well, not a lot, but what 
it does is pretty helpful:

- One minor benefit NSObjectController has is that if you end up with reference 
cycles caused by the bindings system, which is a problem I’ve run into from 
time to time, you can just set the object controller’s content to nil in 
windowWillClose: or a similar place, and break everything’s bindings at once so 
they won’t interfere with the teardown. However, by far the most important 
feature of NSObjectController is:

- NSEditor support 
(https://developer.apple.com/reference/appkit/1654405-nseditor 
). This gives 
you a quick and easy way to solve a very annoying problem. Try building this 
simple application from the Xcode non-storyboard template:

https://gist.github.com/anonymous/207330c13c9144c52381d31421574777 


Try making this application, then binding a text field directly to 
AppDelegate’s “foo” property. Then, connect a button to the saveOrSomething: 
method. Now, build and run the app, type something into the text field, and 
click the button. What do you get? Well, chances are, you saw this:

as this is clicked, foo is ''

The didSet on foo was never called, foo was never updated, and consequently 
when you clicked your save button, out-of-date information got saved. Why is 
this? Because NSTextField doesn’t actually update its bindings until editing 
*ends*, either by the user moving the focus to another UI element by clicking 
on another text field or something, or by the user hitting Return. If you hit 
the Return key after typing something in the field, and *then* hit the button, 
you’ll get the behavior you expect:

did set foo to This is a test
as this is clicked, foo is 'This is a test'

However, this is a pretty annoying UI to use, because it’s very easy to forget 
to end editing before performing some action that depends on the results. You 
can end up thinking you were done with something, closing a document, then 
having to go back and set things up again. If you’ve used macOS/OS X for a long 
time, you’ve probably used a few apps that work like this (Xcode suffered from 
this behavior in certain places for a part of its development), and you’ll know 
how aggravating this can be.

Anyway, NSObjectController can come to our rescue. Change the above code to 
this:

https://gist.github.com/anonymous/c30df983f7603c04cf186b6499c7fac1 


Bind the text field to the object controller instead of to AppDelegate, build 
and run. Type something in the text field, don’t hit Return, and click the 
button. You’ll get:

did set foo to this is a test
as this is clicked, foo is 'this is a test'

Now, when your save method is called, anything that was currently being 
operated upon by a UI element will be updated for you, ensuring that your 
actual save operation writes up-to-date data. Nice!

Note: If you’re using storyboards instead of old-school XIB files, your “File’s 
Owner” will, in most places, be a view controller. NSViewController supports 
the NSEditor methods itself, including commitEditing(), so in this case you 
don’t need an NSObjectController; you can just bind straight to the view 
controller and call commitEditing() on it when necessary. NSObjectController is 
handy in situations where you don’t have an NSViewController, though.

Charles
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: What do NSArrayController, etc. do for me?

2017-01-13 Thread Richard Charles

> On Jan 13, 2017, at 9:30 AM, Richard Charles  wrote:
> 
> 
> You can chain array controller together.

You can chain array controllers together.

Also binding using a controller will work better in Interface Builder than 
binding directly. Binding directly in Interface Builder may have memory 
management issues.

--Richard Charles


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: What do NSArrayController, etc. do for me?

2017-01-13 Thread Charles Srstka
> On Jan 13, 2017, at 6:39 AM, Daryle Walker  wrote:
> 
> If my data model supports KVC/KVO, it seems like I should be able to bind my 
> views to my data directly via Cocoa Bindings, without using NSArrayController 
> or NSObjectController or the like. What advantage do those classes give me?
> 
> Sent from my iPhone

What does NSArrayController do? A heck of a lot:

- Automatic propagation of an NSTableView without your having to implement a 
data source.

- Automatic handling of sorting. So, if you click on a table header, that 
column is sorted for you without your having to worry about it. This is fairly 
sophisticated, as well; If you had column A selected before you clicked on 
column B, it will remember that, so two items that have an identical value for 
column B will get sorted according to their values in column A.

- Automatic selection management. So, you can bind a UI element to the array 
controller’s selected object, and that UI element’s content will change as the 
array controller’s selection changes. Pretty handy for implementing something 
like Mail.app’s UI where selecting a different e-mail message in the left-hand 
pane updates the right-hand pane with the contents of that message.

Charles

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: What do NSArrayController, etc. do for me?

2017-01-13 Thread Richard Charles

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com