Re: What do NSArrayController, etc. do for me?
Re: What do NSArrayController, etc. do for me?
- Subject: Re: What do NSArrayController, etc. do for me?
- From: Charles Srstka <email@hidden>
- Date: Fri, 13 Jan 2017 11:52:43 -0600
> On Jan 13, 2017, at 6:39 AM, Daryle Walker <email@hidden <mailto:email@hidden>> 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 <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 <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 <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 (email@hidden)
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:
This email sent to email@hidden