 |
 |
A cocoa-problem
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Mar 2001
Status:
Offline
|
|
Hellow,
I am coding my first Cocoa-app with a GUI. I am teaching myself cocoa with the Aaron Hillegass-book Cocoa programming for mac os x (edition 1). I am waiting for edition 3 :-)
But I am encountering the following problem. It is not easy to explain but I will try... I hope somebody can help me.
My app is a document based app. It's main class is then the class "MyDocument". In the MyDocument-class I have an object let's say "house". This object house holds NSMutableArrays windows, doors, ...
I added another (controller-)class to add windows to the house, the class "WindowsController". This class is a controller for a sheet that will add a window.
But the problem is to add the window to the house. How do I access the house object in the MyDocument-class ?
If this isn't clear, please tell me :-)
thx in advance,
Jasper
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
With an accessor method, I'd think.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Mar 2001
Status:
Offline
|
|
Problem is how to write this accessor method?
In a document-based app an object other class MyDocument is created by launching the app. And every time you open a new document the app creates a new object of the MyDocument-app. How can I access those instances of MyDocument?
If I can access the object of the MyDocument-class I can easily created an accessor for the house-object.
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
There are a couple of ways you could handle this.
If your controller is either created by the document or another object that knows about the document, then you can just give the controller an instance variable and set that to whatever document you're interested in at the moment (similar to NSTableView's data source).
Alternately, you could take advantage of the responder chain, which NSDocument takes part in.
There's a more detailed discussion on CocoaDev.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Mar 2001
Status:
Offline
|
|
I think I figured out how to solve my problem
Always nice that people here at macnn are so friendly :-) thx guys
|
|
|
| |
|
|
|
 |
|
 |
|
Posting Junkie
Join Date: Dec 2000
Status:
Offline
|
|
Since your "windows" object is an NSArray, if you're thinking of displaying your "windows" in an NSTableView or anything like that, you might want to think of using an NSArrayController. Wire an NSObjectController's "content" outlet to your NSDocument subclass, then bind the NSArrayController to whatever property on the NSDocument subclass points to the "windows" NSArray. Get Info on the NSArrayController and set the Class Name to whatever kind of class is being stored in the "windows" array, and bind each column in your table view to whichever property of that class you'd like to see in that column. Now, you can just wire a button to the "add:" or "remove:" action on the NSArrayController to add or remove an object easily, without even having to reload the table view. You'll also get a bunch of other nice UI features by doing this. The only disadvantage is that the 3 people out there who are still using OS X 10.2 or lower won't be able to use your app.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Mar 2001
Status:
Offline
|
|
I figured out a solution for my problem.
I have class MaterialsController. The purpose of this class is adding or removing objects ("materials") to the array "materialsArray" in the MyDocument class. This is one of the 2 dataSource-methods I used:
- (id)tableView  NSTableView *)aTableView
objectValueForTableColumn  NSTableColumn *)aTableColumn
row  int)rowIndex
{
dc = [NSDocumentController sharedDocumentController];
NSString *identifier = [aTableColumn identifier];
material = [[[dc currentDocument] materialsArray] objectAtIndex:rowIndex];
return [material valueForKey:identifier];
}
Is this a good solution? I don't know because I am experiencing the following problem: After adding one or 2 objects to the materialsArray there is no problem. But when I add a 3th object to the array it all crashes when the tableview is accessing this this method. I don't have a clue why...
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
I don't think your crasher is in that code. But that snippet could contain a bug if each document has its own tableview — background documents would get updated with foreground info. It's not clear if that's the case, but I figure it's worth pointing out.
The crashing bug most likely has to do with your memory management of the objects in the array.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Mar 2001
Status:
Offline
|
|
indeed, the crasher was a deeper error in the codebase. Indeed a memory management-issue... thx
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
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
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|