Newbie: IB bindings versus programmatic bindings
|
|
Thread rating:  |
Jens Vesterdahl - 20 Mar 2008 02:26 GMT Hello all.
I'm new to cocoa programming, and my first project relies heavily on bindings. I've set them up in IB, which seemed like a good idea after reading the "getting started" manuals 'n stuff.
However, now that I'm getting deeper into the project, it seems like IB hides everything away if I want to expand on it. Say I want to add a new custom control and I want to bind to the existing values in the app. They're simply not there, or they're hidden away in some controller. It seems like I'm better off doing things programmatically after all.
What do you seasoned cocoaders do out there? Use IB or write your code by hand?
I'm on xCode 3.0 and MacOS 10.5.2 by the way, if it makes any difference.
Best regards, Jens
Patrick Machielse - 20 Mar 2008 10:47 GMT > What do you seasoned cocoaders do out there? Use IB or write your code > by hand? If you mean: 'should I use bindings or not'? I say: do use bindings. They can eliminate a lot of glue code. Also, they make it relatively easy to add features like smart enabling/disabling of buttons, which is very nice.
It is true that that you have to design your app with bindings in mind. It is also true that IB could be smarter about key paths. It could do a better job at remembering the available key paths, and possibly even find out which ones are available. On the other hand, maintaining bindings has become easier under Xcode 3; the refactoring tool will change your bindings' key paths in the nibs if you change the name of an ivar/accessor.
patrick
Jens Vesterdahl - 20 Mar 2008 14:53 GMT > > What do you seasoned cocoaders do out there? Use IB or write your code > > by hand? [quoted text clipped - 3 lines] > easy to add features like smart enabling/disabling of buttons, which is > very nice. I agree, once set, they're extremely useful and I can get a lot of functionality almost for free just by setting a binding of a control.
My confusion comes from trying to bind a custom view to my existing controllers. Since I asked the question here, I searched and read a lot about the subject, and using the right methods, it should be doable without too much trouble. I'd like to see my custom bindings in IB. Oh well - I suppose it's easy to remember that if I make a custom view, my bindings must be made custom as well.
> It is true that that you have to design your app with bindings in mind. > It is also true that IB could be smarter about key paths. It could do a [quoted text clipped - 3 lines] > change your bindings' key paths in the nibs if you change the name of an > ivar/accessor. I suppose that my main problem is that I was born and raised in the embedded world, where I had to write everything myself. Working with things I don't have to care about takes some getting used to. It's like I expect *everything* to be taken care of in cocoa now :-)
Thanks, Jens
Jens Vesterdahl - 20 Mar 2008 15:22 GMT Allright, it was late yesterday, so I didn't really know what I wanted to ask.
I want to bind values in my custom view to an existing and well functioning array controller. This controller exists in IB only, so how do I bind to its instance? Should I bind to its class? That doesn't sound right.
I should use "bind:toObject ...", this I know. But I figure it should be to an instance, not a class. Should I instantiate the class in a file for this purpose only? Could be useful later on, I suppose.
Jens
Dave Seaman - 20 Mar 2008 16:11 GMT > Allright, it was late yesterday, so I didn't really know what I wanted > to ask.
> I want to bind values in my custom view to an existing and well > functioning array controller. This controller exists in IB only, so how > do I bind to its instance? Should I bind to its class? That doesn't > sound right.
> I should use "bind:toObject ...", this I know. But I figure it should be > to an instance, not a class. > Should I instantiate the class in a file for this purpose only? Could be > useful later on, I suppose. You create an outlet and connect it to the array controller in IB.
 Signature Dave Seaman Oral Arguments in Mumia Abu-Jamal Case heard May 17 U.S. Court of Appeals, Third Circuit <http://www.abu-jamal-news.com/>
Jens Vesterdahl - 20 Mar 2008 17:00 GMT > > I want to bind values in my custom view to an existing and well > > functioning array controller. This controller exists in IB only, so how [quoted text clipped - 7 lines] > > You create an outlet and connect it to the array controller in IB. Mmmm ... yes, but is that enough? On the bindings tab in IB, my custom view still only has two bindings: "Availability/Hidden" and "Parameters/Tool Tip".
What I want is to design my custom view so that my bindings are exposed on the bindings tab in IB. OR a way to programmatically bind to the array controller from inside my custom view.
If I want to access the array controller from anywhere but IB, can I use the name given in the IB Identity panel (is that the instance name?) or do I need to subclass the array controller?
Jens
Dave Seaman - 20 Mar 2008 17:22 GMT >> > I want to bind values in my custom view to an existing and well >> > functioning array controller. This controller exists in IB only, so how [quoted text clipped - 7 lines] >> >> You create an outlet and connect it to the array controller in IB.
> Mmmm ... yes, but is that enough? On the bindings tab in IB, my custom > view still only has two bindings: "Availability/Hidden" and > "Parameters/Tool Tip".
> What I want is to design my custom view so that my bindings are exposed > on the bindings tab in IB. > OR > a way to programmatically bind to the array controller from inside my > custom view.
> If I want to access the array controller from anywhere but IB, can I use > the name given in the IB Identity panel (is that the instance name?) or > do I need to subclass the array controller? You asked for a way to bind to an instance of NSArrayController instead of a class. That's the question I answered. You still need to set up the actual bindings, but you now have something to bind to.
 Signature Dave Seaman Oral Arguments in Mumia Abu-Jamal Case heard May 17 U.S. Court of Appeals, Third Circuit <http://www.abu-jamal-news.com/>
Jens Vesterdahl - 20 Mar 2008 23:54 GMT > >> > I want to bind values in my custom view to an existing and well > >> > functioning array controller. This controller exists in IB only, so how [quoted text clipped - 10 lines] > of a class. That's the question I answered. You still need to set up > the actual bindings, but you now have something to bind to. Ok, I got it now, I think. Now I can bind to the NSArrayController, but setting up the bindings don't work. The bind methods seem to have too little information about the array controller.
It looks more and more like I need to subclass the NSArrayController, which I have suspected for some time. Later on I will need more than just the abstract class.
Thanks.
Jens
Dave Seaman - 21 Mar 2008 00:15 GMT >> >> > I want to bind values in my custom view to an existing and well >> >> > functioning array controller. This controller exists in IB only, so how [quoted text clipped - 10 lines] >> of a class. That's the question I answered. You still need to set up >> the actual bindings, but you now have something to bind to.
> Ok, I got it now, I think. Now I can bind to the NSArrayController, but > setting up the bindings don't work. The bind methods seem to have too > little information about the array controller. What information is missing? You know you need to define the keys in the attributes pane of IB for the NSArrayController before they become visible on the bindings pane, right? I am mainly thinking of IB 3.0 here, because I don't remember clearly how the inspector panes were laid out in older versions of IB, but I remember that the underlying concepts are the same.
> It looks more and more like I need to subclass the NSArrayController, > which I have suspected for some time. Later on I will need more than > just the abstract class. What information are you trying to add?
 Signature Dave Seaman Oral Arguments in Mumia Abu-Jamal Case heard May 17 U.S. Court of Appeals, Third Circuit <http://www.abu-jamal-news.com/>
Jens Vesterdahl - 21 Mar 2008 01:15 GMT Mwahahaaaaa ... it works now! It's done like you all said, but as a rookie, I just didn't get it. No extra controllers or instantiations were needed.
I added an outlet in the Custom View instance and connected this outlet to my arraycontroller in IB. Then I made appropriate bind statements in the custom view's awakeFromNib method:
[self bind: "@viewProperty" toObject:theArrayController withKeyPath:@"selection.arrayControllerProperty" options:nil];
... and that was it!
Very cool, and very simple. Thanks all,
Jens
Patrick Machielse - 20 Mar 2008 21:17 GMT > > You create an outlet and connect it to the array controller in IB. > [quoted text clipped - 7 lines] > a way to programmatically bind to the array controller from inside my > custom view. When you've connected the outlet in IB you can set up the bindings in code inside your
- (void)awakeFromNib {}
method.
It should also be possible to 'expose' the bindings to Interface Builder. This used to require creating an IB Pallette (which are no longer supported) and thus was usually more trouble than it was worth.
This document: <http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit /Protocols/NSKeyValueBindingCreation_Protocol/Reference/Reference.html>
says: "NSView subclasses can expose additional key-value-coding / key-value-observing compliant properties as bindings by calling the class method exposeBinding: for each of the properties. This is typically done in the class's initialize method. By exposing the bindings that an object supports and creating an Interface Builder palette, you can make instances of your own classes bindable in Interface Builder."
But this is obviously dated information now. I'm not sure how / if this situation has changed in IB 3.0. It could be interesting to investigate if IB is smarter about custom bindings now.
Let me know what you find out...
patrick
Jens Vesterdahl - 21 Mar 2008 00:20 GMT > When you've connected the outlet in IB you can set up the bindings in > code inside your [quoted text clipped - 6 lines] > Builder. This used to require creating an IB Pallette (which are no > longer supported) and thus was usually more trouble than it was worth. I noticed that other people say it's not worth it, so I'm programming the bindings by hand. Now it looks like I just need to instatiate the array controller so that my custom view knows about it.
Thanks, Jens
Dave Seaman - 21 Mar 2008 02:24 GMT >> When you've connected the outlet in IB you can set up the bindings in >> code inside your [quoted text clipped - 6 lines] >> Builder. This used to require creating an IB Pallette (which are no >> longer supported) and thus was usually more trouble than it was worth.
> I noticed that other people say it's not worth it, so I'm programming > the bindings by hand. > Now it looks like I just need to instatiate the array controller so that > my custom view knows about it.
> Thanks, > Jens Agreed, you can do this in IB without writing code, unless I am misunderstanding what you are trying to do.
 Signature Dave Seaman Oral Arguments in Mumia Abu-Jamal Case heard May 17 U.S. Court of Appeals, Third Circuit <http://www.abu-jamal-news.com/>
John C. Randolph - 24 Mar 2008 11:51 GMT > What do you seasoned cocoaders do out there? Use IB or write your code by hand? I've been writing apps using the Interface Builder since 1989, and I use IB for everything it can do. The only time I'd resort to making connections or bindings in code is if I'm manipulating the object graph at runtime in response to user actions (IE, making connections that IB can't anticipate.)
-jcr
|
|
|