Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > A cocoa-problem

A cocoa-problem
Thread Tools
jasper_db
Fresh-Faced Recruit
Join Date: Mar 2001
Status: Offline
Apr 30, 2008 , 01:40 PM
 
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
     
Chuckit
Moderator
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Apr 30, 2008 , 01:57 PM
 
With an accessor method, I'd think.
Chuck
___
"But what if it I have a disease of it hurts?"
     
jasper_db
Fresh-Faced Recruit
Join Date: Mar 2001
Status: Offline
Apr 30, 2008 , 02:49 PM
 
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.
     
Chuckit
Moderator
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Apr 30, 2008 , 03:18 PM
 
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
___
"But what if it I have a disease of it hurts?"
     
jasper_db
Fresh-Faced Recruit
Join Date: Mar 2001
Status: Offline
Apr 30, 2008 , 04:14 PM
 
I think I figured out how to solve my problem

Always nice that people here at macnn are so friendly :-) thx guys
     
CharlesS
Posting Junkie
Join Date: Dec 2000
Status: Offline
Apr 30, 2008 , 07:39 PM
 
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.

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist 2.5.2. Macworld - five mice!
     
jasper_db
Fresh-Faced Recruit
Join Date: Mar 2001
Status: Offline
May 1, 2008 , 03:35 AM
 
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)tableViewNSTableView *)aTableView
objectValueForTableColumnNSTableColumn *)aTableColumn
rowint)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...
     
Chuckit
Moderator
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
May 1, 2008 , 03:08 PM
 
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
___
"But what if it I have a disease of it hurts?"
     
jasper_db
Fresh-Faced Recruit
Join Date: Mar 2001
Status: Offline
May 1, 2008 , 04:47 PM
 
indeed, the crasher was a deeper error in the codebase. Indeed a memory management-issue... thx
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:49 AM.
All contents of these forums © 1995-2008 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.6.8 © 2000-2008, Jelsoft Enterprises Ltd., SEO by vBSEO 3.0.0 RC8