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 > OK for multiple controllers?

OK for multiple controllers?
Thread Tools
Forum Regular
Join Date: Nov 2001
Location: Australia
Status: Offline
Reply With Quote
Jun 25, 2002, 12:34 AM
 
The cocoa tutorials in the developer documentation folder describe that applications are made up of multiple view objects, connected to one controller, which is connected to multiple model objects. Is it acceptable to have multiple controller objects?

I have a NSTabView and am thinking of having a controller for each tab item. This would cut down on file size, but would it make coding unnecessarily complex?
     
Fresh-Faced Recruit
Join Date: May 2002
Status: Offline
Reply With Quote
Jun 25, 2002, 03:39 AM
 
You can have as many controllers as you want. However, for your particular situation, I personally would probably use one controller to control all the TabViews, unless each of your Tabs is particularly complex/untrelated-to-the-others. Then I think you should consider separate controllers for each, with some sort of "master" controller to make is easier to manage the TabViews "as a group".

The single controller in the tutorials is mostly to make the tutorial easier to follow.
Love,
The Surfer
     
V0ID  (op)
Forum Regular
Join Date: Nov 2001
Location: Australia
Status: Offline
Reply With Quote
Jul 7, 2002, 01:43 AM
 
And if I have multiple controllers, how can one controller access an object, say an array, that's been created in another controller?
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jul 7, 2002, 01:57 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by V0ID:
<strong>And if I have multiple controllers, how can one controller access an object, say an array, that's been created in another controller?</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">You have to create a class method in your controller with a name such as sharedInstance, which returns a variable holding a single instance of your class. So, if you had a AppController that wanted to talk to your TableViewController, AppController would send TableViewController a sharedInstance message like:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">otherArray = [[TableViewController sharedInstance] dataArray];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Add a sharedInstance static variable to your class like:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">static id sharedInstance = nil;</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">There are various ways to implement sharedInstances, but I like:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">+ sharedInstance
{
if (!sharedInstance)
{
[[self allocWithZone:[[NSApplication sharedApplication] zone]] init];
}
return sharedInstance;
}
- init
{
if (sharedInstance)
{
[super dealloc];
return sharedInstance;
}
self = [super init];
sharedInstance = self;
...
return self;
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">(This is just from memory, but I think it's correct). Hope this helps .

<small>[ 07-07-2002, 03:00 AM: Message edited by: Ibson ]</small>
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 7, 2002, 04:00 AM
 
If you're creating your controllers in IB, you can adapt Ibson's suggestions by having each controller register itself with its class object in its -awakeFromNib method and have your +sharedInstance method return that.

Or you could just connect them with outlets.

<small>[ 07-07-2002, 05:02 AM: Message edited by: Chuckit ]</small>
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
V0ID  (op)
Forum Regular
Join Date: Nov 2001
Location: Australia
Status: Offline
Reply With Quote
Jul 7, 2002, 04:07 AM
 
Thanks! And this is programmatically acceptable? (ie, not considered a quick/dirty hack?)
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jul 7, 2002, 04:28 AM
 
Of course it is!

I have different controllers for each tab in an app I recently wrote, so I could factor out each tab into its own nib... Each tab has quite a few controls on it, and IB was starting to groan under the load.
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jul 7, 2002, 04:31 AM
 
It is perfectly programmatically acceptable—and is in fact used by Apple in its frameworks such as NSApplication's sharedApplication method, and NSDefaults standardUserDefaults method. All return a single instance of an object.
     
V0ID  (op)
Forum Regular
Join Date: Nov 2001
Location: Australia
Status: Offline
Reply With Quote
Jul 7, 2002, 04:59 AM
 
Thankyou all! I'm actually writing in Java ATM (I know, I know, I'll switch to Obj-C once this program is finished...) and I've just written a little test scenario and it seems to all work! I was scared that I'd have to combine all my controllers into one file... <img border="0" title="" alt="[Eek!]" src="eek.gif" /> !
     
   
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
BB 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 09:39 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2