On May 25, 2008, at 3:49 PM, Scott Anguish wrote:
On May 25, 2008, at 3:15 AM, Johnny Lundy wrote:
Well, tell that to the guy who wrote the Currency Converter Using
Bindings tutorial. See the last sentence here:
"This concrete example should help you understand what the value
binding’s configuration implies: The content of the text field is
bound to the value of the exchangeRate key, which Cocoa bindings
finds on the model object using key-value coding. Cocoa bindings
knows to look for that key in the model object that is bound to
thecontent outlet of the controller specified by the Bind to aspect—
in this example, the controller is theNSObjectController instance
you configured earlier, whose content outlet points to the
Converter object you instantiated in the nib file."
And you wonder why I am still confused. That paragraph from Apple
directly contradicts what you just said.
In a real application of any substance you'd likely create your
model object dynamically (from saved data or a new state which you
intend to be able to save and load in the future).
In this _specific_ example (which is what that last sentence says)
the model object is being instantiated in the nib. This is because
the creation of the model object isn't what the example is
attempting to show... it's the connections between the objects and
creating them in IB.
(it isn't a particularly extensive bindings example (purposely), it
is out of date (old Xcode and IB), and needs a total rewrite and
expansion...)
I would say that a lot of the 'beginner' code is a bit like this, if
it does have a View, Controller and Model they are all instantiated in
the NIB, so I can see where some of the confusion comes from, in fact
I've now gotten confused again myself.
Imagine that I have a model with two properties, number and its
square, something much like the currency converter. Ok I've written my
model object, it generates events when things change so if you set the
number, it computes the square and tells anyone listening about it.
View is easy - I have two text boxes.
Then I write a controller which has outlets for the two text boxes,
and connections to the view so it knows when the text boxes update,
and it is able to set properties on a model, and listen to the events
on the model. ie lots of glue code.
So the idea is I type a number in the text box, the controller gets
notified of the change, gets the number, sets the property on the
model. The model computes the square and notifies anyone listening
that it changed, the controller is listening, sees the change, gets
the new square and sets the text box on the view.
That's my setup . So now I put the view and the controller in the NIB
and I don't instantiate a model object in the NIB because I want to do
that in code. This is a standard non-document application.
Where do I make the model object and how do I hook it into the
controller? I start the application, the Files Owner is the singleton
NSApplication, I guess I can override the NIB finished loading method,
make the model object there and now I want to call something on the
controller like ...
-(Controller)hereIsYourModelObject:(Model*)model
where the controller caches the model object and registers to listen
for events. But .. how do I get the controller object in this case to
make the call?
Do I subclass NSApplication, adding an outlet for the Controller, make
the application class my subclass (instead of Application) so that
Files Owner has that outlet and hook that to Files Owner in IB ? In
that way when the NIB is instantiated, Files Owner's Controller outlet
will be set to my Controller, I can then use nsapp.controller
directly. (actually can I, is NSApp a global static variable which can
be used from any class as I'll be in an awakeFromNIB() class method
somewhere)?
Or was this entire thing the wrong way to do it? Should I have had TWO
NIBS, one with nothing but the menu in it, and one with my Controller
and View. The second NIB has, as Files Owner type, the Model and I set
up the connections to it in IB. Then, when the application starts, I
have an awakeFromNIB() method in something in the first NIB where I
create a Model object and then load the second NIB with my Model as
the Files Owner object?
I know this is a bit silly, for a singleton like that you may as well
just put the model in there, but I'm trying to figure out if you
didn't, what the right way to hook it up to the controller is later. _______________________________________________
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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [EMAIL PROTECTED]