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 > How to call functions between different classes? *newbie*

How to call functions between different classes? *newbie*
Thread Tools
rudynorff
Junior Member
Join Date: Oct 2000
Location: Germany
Status: Offline
Reply With Quote
Nov 20, 2002, 02:33 PM
 
Hello,

I hope you can help me out. I have created an application with 3 classes.

- Controller (manages the main window or mainmenu.nib)
- edController (manages another window)
- VarHolder (stores variables)

I created an instance of edController from my controller object.
I then went on to create an object for the VarHolder class, also from my ontroller class.

So both objects are created in my controller object.

Now I want to store variables from a textfield from my edController object (which I called edcontroller) into my VarHolder object (called varHolder).

As a newbie I wrote something like this in my edcontroller object when a button is pushed:
[varHolder setMyString: [myGreatTextField stringValue]];
Almost forgot: setMyString is an accesor method, but you guys probably know better than I do

It doesn't work though, because I created the instance of VarHolder in the controller object and the compiler tells me that varHolder cannot be found from edcontroller!

Thanks for any answers!

- rudy
     
yeslekmc
Junior Member
Join Date: Aug 2002
Location: Northeastern NV, USA
Status: Offline
Reply With Quote
Nov 20, 2002, 04:09 PM
 
Does the file containing the reference to varHolder import the header file for varHolder at the top of the code?
     
00101001
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
Nov 20, 2002, 04:47 PM
 
varHolder is a class...

is setMyString a class or an instance method?
     
rudynorff  (op)
Junior Member
Join Date: Oct 2000
Location: Germany
Status: Offline
Reply With Quote
Nov 20, 2002, 05:23 PM
 
Originally posted by 00101001:
varHolder is a class...

is setMyString a class or an instance method?

setMyString is an instance method:

- (void)setMyString: (NSString *)s;

thanks for your replies!!!
- rudy
     
Rickster
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Nov 20, 2002, 06:27 PM
 
You can't get at your VarHolder instance from your edController instance without explicitly giving it access.

If you want to have a single instance of VarHolder that provides services to numerous other objects in the app, you could follow the pattern used by NSApplication, NSFontManager, and other classes that have shared instances and implement a class method like this:

Code:
static VarHolder *SharedInstance = nil; + (VarHolder *)sharedVarHolder; { if (SharedInstance == nil) SharedInstance = [[self alloc] init]; return SharedInstance; }
Then you can call [VarHolder sharedVarHolder] anywhere you need access to this shared instance.


However, it sounds a bit like you might want to reevaluate your app's design. What are the purposes of these three classes? Does VarHolder actually do anything, or does it, as its name implies, just hold values for the two controller classes to work with? Cocoa is designed around the Model-View-Controller paradigm, and it works best if you stick with it: make sure the "business logic" is in the model class, and the controllers pass along data and instructions from the UI.


By the way, you'll have less confusion between instances and classes and variables (et cetera) if you follow common naming conventions. (You'll also find it easier to use some of Cocoa's more advanced features.) See this article for a good summary.
Rick Roe
icons.cx | weblog
     
rudynorff  (op)
Junior Member
Join Date: Oct 2000
Location: Germany
Status: Offline
Reply With Quote
Nov 21, 2002, 01:27 PM
 
Hello,

thanks for your reply. This is what I actually planned to do:
(please don't laugh as I'm an absolute beginner).

In the nib of my controller class I have a tableview.
In the nibfile of the edController class I created a window which lets the user enter a string. when the user clicks a button I want to set myString in the VarHolder class to the stringValue of the textfield. Then the Controller class gets the string from VarHolder and displays it in the table. this is how I wanted to do it. there probably is a better way to do it. if there is please tell me, but please also tell me if it's possible to do it my way or if it's simply nonsense!

thanks for your time!
rudy
     
hayesk
Guest
Status:
Reply With Quote
Nov 21, 2002, 04:57 PM
 
Originally posted by rudynorff:
In the nib of my controller class I have a tableview.
In the nibfile of the edController class I created a window which lets the user enter a string. when the user clicks a button I want to set myString in the VarHolder class to the stringValue of the textfield. Then the Controller class gets the string from VarHolder and displays it in the table. this is how I wanted to do it. there probably is a better way to do it.
I'm new at this too but I think you can do this. When Controller instantiates edController, it can pass itself as a reference to the init method. Then edController can store the reference to Controller.

Controller should own the object varHolder and have a method to set it. edController then has an action method to receive the button click and in it, calls the method in Controller passing the value of the text field. The method in Controller receives the text field and stores it in varHolder.
     
   
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
Top
Privacy Policy
All times are GMT -4. The time now is 04:25 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,