 |
 |
Calling methods
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Ok I have run into a small problem. I have searched the web and can find no answer, probably because I am looking to hard. Here is my problem:
How do you call a method of one interface from another interface?
EX: Interface A needs to call method X in Interface B.
I hope someone here has the information I need. Thanks!
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Aug 2001
Status:
Offline
|
|
Um, what language? (I haven't run across any where interfaces can call methods, so I'm not going to guess, unless you meant "object" instead of "interface")
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
LOL  Sorry about that. It is in Objective-C. I think I am confusing both myself and everyone else. I am at work right now so I cant get code. I will try an example:
You have 2 windows(.m files) in objective-c. When one function in window A completes, then you need to call a function that is in the .m file for window B that will set a TextField in window B.
I think there is a simple way of doing this, but I cannot figure it out... Am I making sense now?
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Dec 2003
Status:
Offline
|
|
OK, say the object handling Window A is of class WinAObject. There you have an IBOutlet NSTextField *textField; and you declare and implement a method to alter it, such as
Code:
- (void) setTextField: (NSString *)newVal;
In the object handling Window B, you #import "WinAObject.h" and have a WinAObject *otherWindow that at some point you set to refer to the instance for Window A.
Then you just call
Code:
[otherWindow setTextField: aString];
That probably was a very unclear explanation. But you can probably get a better sense of it by creating a simple sample app.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Ahh ok that makes perfect sense I think. Does this sound right?
A.m
#import "B.m"
...
[windowB setText:text];
...
B.m
...
-(void) setText:text //or whatever
{
[textField setStringValue:text];
}
...
I figure I was missing setting up an object for the window. I will try that tonight when i get off work and see what happens. If it works, then you will have solved my problems!! Will that cause another window to appear though?
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Originally posted by djohnson:
I figure I was missing setting up an object for the window. I will try that tonight when i get off work and see what happens. If it works, then you will have solved my problems!! Will that cause another window to appear though?
Only if you create another window. What you need is a pointer to the address of your existing controller object, which you could set up in IB if they're both in the same nib file.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Ok that last part lost me... How do you setup a pointer to the address of my existing controller object? Yes they are in the same nib file.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Originally posted by djohnson:
Ok that last part lost me... How do you setup a pointer to the address of my existing controller object? Yes they are in the same nib file.
Create an outlet.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Ok I see how creating an outlet will connect them, but what do you connect the outlet to?
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Originally posted by djohnson:
Ok I see how creating an outlet will connect them, but what do you connect the outlet to?
The other object in your nib. Like this:

|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Oh... I learn something new everyday  Thanks! I will try that in the morning and see what happens. It does make sense though, connecting one object to another means they can communicate which is what objective-c is all about. Thanks!
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
Originally posted by djohnson:
Ahh ok that makes perfect sense I think. Does this sound right?
A.m
#import "B.m"
...
[windowB setText:text];
...
B.m
...
-(void) setText:text //or whatever
{
[textField setStringValue:text];
}
...
I figure I was missing setting up an object for the window. I will try that tonight when i get off work and see what happens. If it works, then you will have solved my problems!! Will that cause another window to appear though?
Don't import the ".m" file, import the ".h" file.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Yes I know... just a mistype was all. Anyways, I have tried this and now am getting that the "caller" class does not know the other class. I do have the ".h" included. Here is the code I have to call the function:
In the .h
IBOutlet prefsWindow *prefConn;
...
In the .m
[prefsConn update];
When I declare the IBOutlet and build, it tells me that prefsWindow is not known. Any ideas?
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Dec 2003
Status:
Offline
|
|
have you put
at the top of the .h file?
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Umm... If you are meaning the first .h... of course. I changed the IBOutlet to type id and it is working! Of course I had to change it in the .nib file. Thanks for the help!
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

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