|
|
How to call functions between different classes? *newbie*
|
|
|
|
Junior Member
Join Date: Oct 2000
Location: Germany
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Aug 2002
Location: Northeastern NV, USA
Status:
Offline
|
|
Does the file containing the reference to varHolder import the header file for varHolder at the top of the code?
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jan 2001
Status:
Offline
|
|
varHolder is a class...
is setMyString a class or an instance method?
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Oct 2000
Location: Germany
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Oct 2000
Location: Germany
Status:
Offline
|
|
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
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|