Re: Binding to a view's model object
Re: Binding to a view's model object
- Subject: Re: Binding to a view's model object
- From: Keary Suska <email@hidden>
- Date: Sun, 25 Jan 2015 08:15:54 -0700
On Jan 25, 2015, at 3:34 AM, Roland King <email@hidden> wrote:
> I have a xib with a top-level view and a bunch of subviews which represents one view of a given model object. The top-level NSView subclass has a readwrite property which is the model object of which it's a view. I thought this was a pretty standard pattern, especially in OSX which only just recently started to really make use of view controllers.
There is nothing standard about this pattern. It is not, AFAIK, a pattern at all, but an anti-pattern. Previous to a view controller the only "view" controller was a window controller, and finer grained control was necessary. For this very purpose it is the view controller, and not its view, which tends to "hold" the model object. Hence Xcode is balking at your attempts.
> Many of the subviews just show strings and other values from the model object, and some of them allow editing as well. So what I wanted to do is use bindings to bind modelObject.someStringProperty to the value of one of the text fields, for instance. Doing that would take care of 90% of the easy properties on the model, leaving just a few I'd need to hand-code. However IB doesn't let me make bindings to a property of the top-level view. I can bind to files owner, the shared defaults controller and the application only, or to any other standalone Object I put in the NIB, but none of those help.
Yes--that's MVC. Views should only bind to controllers, not other views.
> The view in this case is going in a stack view, which wants an NSView. I thought about moving the logic into a view controller which could be the Files Owner, but I would have then to separately retain those view controllers as their views don't retain them and the stackview just wants the view, so that feels a little clunky. The views are meant to be reusable, hence the single readwrite model object property on the view.
Someone has to own the xib, why not have it serve up the model object? Or own the NSViewController(s) that do? These would represent standard patterns and don't seem clunky. Note, however, that objects of NSWindowController and NSViewController have some built-in binding break-down when they are released, and are usually more bindings-friendly.
> Is there a way to do these bindings? Can it be done in IB if so or do I need to call bind:toObject:withKeyPath:options: Is there some other object I could put in the NIB which would intermediate this for me?
Well, it's hard to say since you haven't listed the items that you don't believe you can use bindings for, but in general, yes, more or less.
HTH,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
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