Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 09-19-2004, 04:02 PM   #1
caifara
Prospect
 
Join Date: Sep 2004
Posts: 5
newbie trouble with cocoa

Hi,

I'm quite a newbie to cocoa, and I really ran into some problems which I can't solve on my own. I also looked intensively on the internet to find some answers, but I can't seem to find a solution.

So, I'm gonna try it here...

I'm making an application (well, trying to) for an agenda my courses. All data comes from a mysql db)
I've put a pop up in a window with some schoolyears in it. And its controller-class (yearsController) also controls a table with the same schoolyears, which is in a tabview below the pop up.
There is another tab, with a table, and in this table I've put all the courses I give in that schoolyear.
This table has another controller (coursesController).

What I want to do is:
If another year is chosen in the popup, a 'loadYear' action happens (in the yearsController), and the courses of that year are loaded from db.
The problem is that I can't get these courses in the table-view of coursesController because if I get an instance of coursesController in yearsController, it's not the same object as the one showing the tableview at startup (also an instance of coursesController).

Now, I hope that this is somewhat clear and maybe very, very obvious to some of you guys! I would really appreciate an answer! (this problem is bothering me for some days now)
caifara is offline   Reply With Quote
Old 09-19-2004, 10:01 PM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 31,941
It sounds like you haven't properly followed the model-view-controller paradigm with your app. A controller should not "have" a tableview. The view objects should be in a separate class. The data should be in a model class. The only thing the controller does is act as an intermediary between the model and the view.

You should look through all of the various examples that Apple supplies with the developer tools. There are examples of the sort of thing you are trying to do.

And you really shouldn't try to learn Cocoa without the aid of one or more books on the subject. See www.cocoadev.com for book recommendations (and lots else). Once you follow through the examples in the books, you will see how to architect your own application.

If you want there to be only one instance of one type of object (e.g. your courses view) , then you could have a method in that class that returns the pointer to the one instance created at startup.
hayne is offline   Reply With Quote
Old 09-20-2004, 04:02 PM   #3
caifara
Prospect
 
Join Date: Sep 2004
Posts: 5
First of all, thank you for answering...

Ok, so I'm gonna buy me a book (learning cocoa 2nd edition) seems to be available here in Belgium.

But I would like to go on about it, if someone still wants to answer off course.

Some more explanation, because maybe, I don't quite follow

I've got a model, from which i get the courses (by year).

At init, my coursescontroller gets those, and in the nib, I created the coursesController as dataSource.

All that works.

The problem is when I try to update the year (and thus also the courses (as they depend on the year)), the coursesController doesn't get the new courses.

So I thought, maybe I'll add a tableview (to be able to use reloadData) and let my yearsController start a function in the coursesController (that accesses the model) (but that gave me that extra instance, which I don't care about).

So this tableview came later.

How can I implement a coursesController action when some yearsController action starts? Or should I do this completely different?

I've got the impression that my data comes from my model... But I'm making a mistake here.

(I did see a lot of howto's, but they allways have one controller for one table, I can do that too, I tried it with a button next to the table that doesn't want to be updated. this button started an action in coursesController with a [coursesTable reloadData], and it worked...)

Please help...
(and I am going to read that book from all the way (twice at least )
caifara is offline   Reply With Quote
Old 09-20-2004, 08:57 PM   #4
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 31,941
I would probably do it something like this:

View class
- has table displaying data

Model class
- provides the data upon demand in various formats

Controller class
- has widgets for choosing years etc
- has a pointer to the model object (note that object variables in Cocoa are pointers)
- has a pointer to the view object

The widget for choosing the year (which is part of the controller) calls a method to tell the view to update itself when the year changes. It gets the appropriate data from the model and passes this data as a parameter to the update call.
hayne is offline   Reply With Quote
Old 09-21-2004, 08:30 AM   #5
caifara
Prospect
 
Join Date: Sep 2004
Posts: 5
Ok, I allmost got it the same way. I got rid of the courses variable, and now the needed data comes from the model, every time the view-class asks for it.

Now the problem remains because the years-widget can't be in this class (it has to stay in the yearsController) because a lot of other things change when one year changes (ceveral tables). And I read that each table should have it's own controller.

So now I can call my coursesController from my yearsController, but it has to point to the right instance of coursesController.

Where can I read stuff to do this? I found this SEL thing, but not enough examples to go on with it (this is how it should be done right?).

guess I'm still missing something...

(the book is ordered :-) )
caifara is offline   Reply With Quote
Old 09-21-2004, 12:57 PM   #6
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 31,941
I'm not sure, but it sounds like your difficulties are that you don't have a pointer to the coursesController in the yearsController object. You would normally pass such a pointer as a parameter when you create the yearsController.
hayne is offline   Reply With Quote
Old 10-06-2004, 02:24 PM   #7
caifara
Prospect
 
Join Date: Sep 2004
Posts: 5
Ok, solved it in the end (and that without still having the book I ordered)

I had to use the Notification center. yeehaaaa.... at last!
(I used this article: http://www.cocoadevcentral.com/articles/000029.php )

@Hayne: thx for answering very fast after each of my posts!
caifara is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 12:42 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.