|
|
Saving Panel Data
|
|
|
|
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status:
Offline
|
|
Greetings,
I created a panel with a tabView embedded in it. The data contained in the panel is unique to each file saved by the user.
I cannot figure out how to write this data to the file I am saving with my other data. I assume I should save the data each time the panel closes, but I dunno where to go from there.
Any help?
|
|
|
|
|
|
|
|
|
Senior User
Join Date: Feb 2003
Location: USA
Status:
Offline
|
|
|
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status:
Offline
|
|
I am usind NSCoding for all of my data in tableviews. I have attached a screenshot of what i am trying to save.
I created a class that has a variable for each item on that panel, but I don't really know how I should tie it into my MyDocument class so the data saves.
Am I making any sense?
Thanks for the help.
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status:
Offline
|
|
I think I need to subclass NSWindowController. ugh.
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
Originally posted by tikki:
I think I need to subclass NSWindowController. ugh.
are you using encodeWithCoder and initWithCoder to save and retrieve your documents?
If so, you must have some single class that is being encoded/decoded?
And you're storing the contents of each of those fields in an NSString or something like that?
If so you'll have an initWithCoder and encodeWithCoder method in the class implementation that is being saved, something like:
Code:
- (id)initWithCoder:(NSCoder *)coder
{
[self setCourseName:[coder decodeObject]];
[self setLocation:[coder decodeObject]];
[coder decodeValueOfObjCType:"i" at:&numberOfPeople];
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:[self courseName]];
[coder encodeObject:[self location]];
[coder encodeValueOfObjCType:"i" at:&numberOfPeople];
}
(This assumes that somewhere you have code that extracts data from your fields into the objects "courseName" and "location". Otherwise, you could theoretically encode/decode directly from the field content itself).
Note the two forms of encoding/decoding. One is for objects (eg, NSString, as above) the other is for C variable types (eg, integer, above).
You then invoke the decoding using something like the following:
Code:
newObject = [NSUnarchiver unarchiveObjectWithFile:filename];
if ( newObject )
{
[newObject retain];
...
...
...
}
and invoke the encoding using somehting like:
Code:
[NSArchiver archiveRootObject:objectToSave toFile:path];
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status:
Offline
|
|
Originally posted by Brass:
are you using encodeWithCoder and initWithCoder to save and retrieve your documents?
Yes.
If so, you must have some single class that is being encoded/decoded?
And you're storing the contents of each of those fields in an NSString or something like that?
Yes. it's an NSMutableArray that holds the values of two other NSMutableArrays (one for each table in the main window)
If so you'll have an initWithCoder and encodeWithCoder method in the class implementation that is being saved, something like:
Code:
- (id)initWithCoder:(NSCoder *)coder
{
[self setCourseName:[coder decodeObject]];
[self setLocation:[coder decodeObject]];
[coder decodeValueOfObjCType:"i" at:&numberOfPeople];
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:[self courseName]];
[coder encodeObject:[self location]];
[coder encodeValueOfObjCType:"i" at:&numberOfPeople];
}
(This assumes that somewhere you have code that extracts data from your fields into the objects "courseName" and "location". Otherwise, you could theoretically encode/decode directly from the field content itself).
Note the two forms of encoding/decoding. One is for objects (eg, NSString, as above) the other is for C variable types (eg, integer, above).
You then invoke the decoding using something like the following:
Code:
newObject = [NSUnarchiver unarchiveObjectWithFile:filename];
if ( newObject )
{
[newObject retain];
...
...
...
}
and invoke the encoding using somehting like:
Code:
[NSArchiver archiveRootObject:objectToSave toFile:path];
[/B][/QUOTE]
Yes, I have my STClassInformation class set up exactly like that, but how do I get all that to work in my MyDocument class.
When the user saves/opens the document, I don't know how to get the panel's data to save with the table view stuff. At this point my STClassInformation has no relation to the panel at all.
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
What is the data structure you're using? You mentioned that you are encoding/decoding an NSMutableArray. Is that an instance variable in your STClassInformation class?
Are you using
newObject = [NSUnarchiver unarchiveObjectWithFile:filename]
&
[NSArchiver archiveRootObject:objectToSave toFileath];
to encode/decode your STClassInformation object, or just the NSMutableArray object?
Ie, are the "newObject" and "objectToSave" in my example of which class, NSMutableArray, or STClassInformation?
Perhaps you could post some sample code?
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status:
Offline
|
|
here's a basic throwdown of what I have.
MyDocument - saves, loads data, add/deletes rows from the two tables.
STClassInformation - class for the stuff in that panel (see screenshot above)
STGrade - each time a person adds a value to the grades table, its an instance of this. Just two NSStrings. The instances are saved in an NSMutableArray called gradeList
STAssignment - each time a person adds a value to the assignment table, its of this. Again, just more NSSTrings. The instances are saved in an NSMutableArray called assignmentList
here is how I do my encoding/decoding in each of those three previous classes.
Code:
- (id)initWithCoder:(NSCoder *)coder {
[super init];
[self setAssignmentType:[coder decodeObject]];
[self setGrade:[coder decodeObject]];
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:[self assignmentType]];
[coder encodeObject:[self grade]];
}
Here is my saving stuff. At this point it basically takes assignmentList and gradeList and consolidates them into an NSMutableArray called dataStuff (for lack of a better phrasing).
Code:
- (NSData *) dataRepresentationOfType:(NSString *)aType
{
[oTableView deselectAll:nil];
_dataStuff = [[NSMutableDictionary alloc] init];
[_dataStuff setObject:_assignmentList forKey:@"assignmentListKey"];
[_dataStuff setObject:_gradeList forKey:@"gradeListKey"];
return [NSArchiver archivedDataWithRootObject:_dataStuff];
}
I doubt its the most elegant code you've ever seen, but its a start.
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
It's a little different to how I'd do it, but is there any reason why you can't just add the other items to your NSMutableDictionary?
Eg
Code:
_dataStuff = [[NSMutableDictionary alloc] init];
[_dataStuff setObject:_assignmentList forKey:@"assignmentListKey"];
[_dataStuff setObject:_gradeList forKey:@"gradeListKey"];
[_dataStuff setObject:_courseName forKey:@"courseNameKey"];
[_dataStuff setObject:_location forKey:@"locationKey"];
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status:
Offline
|
|
Doesn't that entail just creating a bunch of outlets to the Textfields in my MyDocument class?
If I am understanding correctly, I would just forget about my STClassInformation?
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
Originally posted by tikki:
Doesn't that entail just creating a bunch of outlets to the Textfields in my MyDocument class?
If I am understanding correctly, I would just forget about my STClassInformation?
Either way would work. I can't recommend one way or the other, because I don't know what you're planning on doing in the rest of your code.
However, what I would usually do, is instead of using an NSMutableArray, I would create another class (perhaps this is what your STClassInfo is) which stores all of those items in it's instance variables. Then instead of archiving/unarchiving an NSMutableArray, you archive/unarchive an STClassInfo object.
Also, I have to admit that I'm not sure what you're using the "dataRepresentationOfType" method for. I've never used that in archiving/unarchiving anything.
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status:
Offline
|
|
Originally posted by Brass:
Also, I have to admit that I'm not sure what you're using the "dataRepresentationOfType" method for. I've never used that in archiving/unarchiving anything.
Probably because I don't know what I am doing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|