PDA

View Full Version : Cocoa Bindings Saving/Loading Help




Soulstorm
Aug 31, 2007, 05:51 AM
I have made a simple program, that consists of an NSTableView that has columns that include text fields, sliders, and an Image view. I built it to make some simple experiments with Cocoa bindings, as I am learning them now.

All are well, the new entries are properly handled, and I can change the imageview contents to any picture I like.

The problem comes at the point where I quit the program. That's when the saving functions are called, and I am sure that I have done something wrong, because when I reopen the program (where the data should be automatically loaded), all I get are empty entries!

Can anyone check the project? It's really simple and small. Any comment would be great.



kainjow
Aug 31, 2007, 06:37 AM
In testClass, when initWithCoder is called, it is working with the ivar properties but properties is only created in init. This fixes it:

- (void) setProperties: (NSDictionary *)newProperties
{
if (properties == nil)
properties= [[NSMutableDictionary alloc] init];
[properties setDictionary:newProperties];
}

You should use *real* setters and getters, and then you would have not gotten this problem :)

Soulstorm
Aug 31, 2007, 07:07 AM
Thanks, kainjow, that corrected my problem.

Now, for another question. In the same project, let's say I have 2 or 3 pictures inside my project. In the field where the picture is displayed, I want an arbitrary picture to be displayed, whose location will be in the "name" field.

For example, if I had an image named "test.jpg" in the project and in the program I went in one entry and in the field I typed "test.jpg", the image to the right (and in the ImageView) would change to reflect the image named "test.jpg" and not the default one.

Any idea on how to do this using bindings?

kainjow
Aug 31, 2007, 07:48 AM
You should be able to do this with NSValueTransformer (http://developer.apple.com/documentation/Cocoa/Conceptual/ValueTransformers/index.html). Specifically, see the Writing a Custom Value Transformer section.

Soulstorm
Aug 31, 2007, 10:01 AM
Thanks, kainjow.

.. but what is the mistake here? I am building a thought organizer, with 2 nstableviews. 1 that holds the days, and the other one that holds the projects of a particular day. No matter how hard I try, I cannot get the project tableview to work, even if I have connected it correctly... The days table view works fine. Can anyone take a look at it, please?

kainjow
Aug 31, 2007, 10:25 AM
First of all, please post the specific problem you are having so we don't have to hunt around to find it (although it was obvious when running your project).

Second, you need to initialize your variables. Bindings is accessing the "projects" array within the day class but you never created it.

This stuff is Cocoa 101. Make sure you know the fundamentals of setting up your classes' ivars and accessor methods before trying to understand bindings. You should go back and review the basics.

Soulstorm
Aug 31, 2007, 11:13 AM
First of all, please post the specific problem you are having so we don't have to hunt around to find it (although it was obvious when running your project).

Second, you need to initialize your variables. Bindings is accessing the "projects" array within the day class but you never created it.

This stuff is Cocoa 101. Make sure you know the fundamentals of setting up your classes' ivars and accessor methods before trying to understand bindings. You should go back and review the basics.


Please don't be mean :)

Heh, I am programming in Cocoa for approx 2 years, having made applications that are used by thousands of people. But having programmed today various projects for approx 14 hours continuously, it's natural to become careless and forgetful.

Cocoa bindings are a new topic for me, since I avoided it for so much time and when I see some errors, it's difficult to imagine that I would have made such a basic mistake.

Anyway, thanks a lot for taking the time to view my code.