 |
 |
OK for multiple controllers?
|
 |
|
 |
|
Forum Regular
Join Date: Nov 2001
Location: Australia
Status:
Offline
|
|
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
|
|
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
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Nov 2001
Location: Australia
Status:
Offline
|
|
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
|
|
</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
|
|
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'."
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Nov 2001
Location: Australia
Status:
Offline
|
|
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
|
|
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
|
|
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.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Nov 2001
Location: Australia
Status:
Offline
|
|
 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" /> !
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

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